JSON to CSV using Python [closed]












-3















been playing with Python recently and whilst has a lot of success, one thing is driving me nuts - how Python handles JSON!



I'm trying to output this into a CSV and for simple JSON (without multiple objects) it's all fine, but with the below example I just can't get it to work. I get errors all over the place either key errors or dict vs string errors - and then they differ even more when trying to write to csv either using csv writer or pandas.
So I'm deliberately avoiding pasting all my attempts in an attempt to see if there is a standard way to approach this without influencing a direction which I've been historically trying to take :)



JSON Example (response from a URL) using response = requests.get(url)



{
"body": {
"stores": [
{
"id": "1002",
"groupId": "aberdeen",
"displayName": "Aberdeen",
"link": "/store/aberdeen",
"address": "123, TheRoad, A24 8EN, Aberdeen",
"Url": "https://web.co.uk",
"other": false,
"other1": null
},
{
"id": "1234",
"groupId": "Basingstoke",
"displayName": "Basingstoke",
"link": "/store/Basingstoke",
"address": "Union Square, The Square, BA11 5RG, Basingstoke",
"Url": "https://web.co.uk",
"other": false,
"other1": null
},...


Does anyone have any idea on how to achieve this?
Ideally I'd like a CSV export as below:



ID, GroupID, DisplayName



8014, aberdeen, Aberdeen



8018, Basingstoke, Basingstoke



...



Thanks
Immy










share|improve this question













closed as off-topic by jonrsharpe, Lie Ryan, pirho, P. Camilleri, Deadpool Nov 21 '18 at 20:24


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – jonrsharpe, pirho, P. Camilleri

If this question can be reworded to fit the rules in the help center, please edit the question.












  • 1





    You've done a good job explaining what you want to do. Now include the code you've written so far and what it outputs, so we can help you.

    – Daniel Farrell
    Nov 21 '18 at 16:21
















-3















been playing with Python recently and whilst has a lot of success, one thing is driving me nuts - how Python handles JSON!



I'm trying to output this into a CSV and for simple JSON (without multiple objects) it's all fine, but with the below example I just can't get it to work. I get errors all over the place either key errors or dict vs string errors - and then they differ even more when trying to write to csv either using csv writer or pandas.
So I'm deliberately avoiding pasting all my attempts in an attempt to see if there is a standard way to approach this without influencing a direction which I've been historically trying to take :)



JSON Example (response from a URL) using response = requests.get(url)



{
"body": {
"stores": [
{
"id": "1002",
"groupId": "aberdeen",
"displayName": "Aberdeen",
"link": "/store/aberdeen",
"address": "123, TheRoad, A24 8EN, Aberdeen",
"Url": "https://web.co.uk",
"other": false,
"other1": null
},
{
"id": "1234",
"groupId": "Basingstoke",
"displayName": "Basingstoke",
"link": "/store/Basingstoke",
"address": "Union Square, The Square, BA11 5RG, Basingstoke",
"Url": "https://web.co.uk",
"other": false,
"other1": null
},...


Does anyone have any idea on how to achieve this?
Ideally I'd like a CSV export as below:



ID, GroupID, DisplayName



8014, aberdeen, Aberdeen



8018, Basingstoke, Basingstoke



...



Thanks
Immy










share|improve this question













closed as off-topic by jonrsharpe, Lie Ryan, pirho, P. Camilleri, Deadpool Nov 21 '18 at 20:24


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – jonrsharpe, pirho, P. Camilleri

If this question can be reworded to fit the rules in the help center, please edit the question.












  • 1





    You've done a good job explaining what you want to do. Now include the code you've written so far and what it outputs, so we can help you.

    – Daniel Farrell
    Nov 21 '18 at 16:21














-3












-3








-3








been playing with Python recently and whilst has a lot of success, one thing is driving me nuts - how Python handles JSON!



I'm trying to output this into a CSV and for simple JSON (without multiple objects) it's all fine, but with the below example I just can't get it to work. I get errors all over the place either key errors or dict vs string errors - and then they differ even more when trying to write to csv either using csv writer or pandas.
So I'm deliberately avoiding pasting all my attempts in an attempt to see if there is a standard way to approach this without influencing a direction which I've been historically trying to take :)



JSON Example (response from a URL) using response = requests.get(url)



{
"body": {
"stores": [
{
"id": "1002",
"groupId": "aberdeen",
"displayName": "Aberdeen",
"link": "/store/aberdeen",
"address": "123, TheRoad, A24 8EN, Aberdeen",
"Url": "https://web.co.uk",
"other": false,
"other1": null
},
{
"id": "1234",
"groupId": "Basingstoke",
"displayName": "Basingstoke",
"link": "/store/Basingstoke",
"address": "Union Square, The Square, BA11 5RG, Basingstoke",
"Url": "https://web.co.uk",
"other": false,
"other1": null
},...


Does anyone have any idea on how to achieve this?
Ideally I'd like a CSV export as below:



ID, GroupID, DisplayName



8014, aberdeen, Aberdeen



8018, Basingstoke, Basingstoke



...



Thanks
Immy










share|improve this question














been playing with Python recently and whilst has a lot of success, one thing is driving me nuts - how Python handles JSON!



I'm trying to output this into a CSV and for simple JSON (without multiple objects) it's all fine, but with the below example I just can't get it to work. I get errors all over the place either key errors or dict vs string errors - and then they differ even more when trying to write to csv either using csv writer or pandas.
So I'm deliberately avoiding pasting all my attempts in an attempt to see if there is a standard way to approach this without influencing a direction which I've been historically trying to take :)



JSON Example (response from a URL) using response = requests.get(url)



{
"body": {
"stores": [
{
"id": "1002",
"groupId": "aberdeen",
"displayName": "Aberdeen",
"link": "/store/aberdeen",
"address": "123, TheRoad, A24 8EN, Aberdeen",
"Url": "https://web.co.uk",
"other": false,
"other1": null
},
{
"id": "1234",
"groupId": "Basingstoke",
"displayName": "Basingstoke",
"link": "/store/Basingstoke",
"address": "Union Square, The Square, BA11 5RG, Basingstoke",
"Url": "https://web.co.uk",
"other": false,
"other1": null
},...


Does anyone have any idea on how to achieve this?
Ideally I'd like a CSV export as below:



ID, GroupID, DisplayName



8014, aberdeen, Aberdeen



8018, Basingstoke, Basingstoke



...



Thanks
Immy







python json csv






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 16:17









Imtiaz UllahImtiaz Ullah

83




83




closed as off-topic by jonrsharpe, Lie Ryan, pirho, P. Camilleri, Deadpool Nov 21 '18 at 20:24


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – jonrsharpe, pirho, P. Camilleri

If this question can be reworded to fit the rules in the help center, please edit the question.







closed as off-topic by jonrsharpe, Lie Ryan, pirho, P. Camilleri, Deadpool Nov 21 '18 at 20:24


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – jonrsharpe, pirho, P. Camilleri

If this question can be reworded to fit the rules in the help center, please edit the question.








  • 1





    You've done a good job explaining what you want to do. Now include the code you've written so far and what it outputs, so we can help you.

    – Daniel Farrell
    Nov 21 '18 at 16:21














  • 1





    You've done a good job explaining what you want to do. Now include the code you've written so far and what it outputs, so we can help you.

    – Daniel Farrell
    Nov 21 '18 at 16:21








1




1





You've done a good job explaining what you want to do. Now include the code you've written so far and what it outputs, so we can help you.

– Daniel Farrell
Nov 21 '18 at 16:21





You've done a good job explaining what you want to do. Now include the code you've written so far and what it outputs, so we can help you.

– Daniel Farrell
Nov 21 '18 at 16:21












1 Answer
1






active

oldest

votes


















0














import pandas as pd
pd.DataFrame(d['body']['stores']).to_csv('my_file.csv')





share|improve this answer
























  • Conner you legend! I can't believe it's just a line of code - I've spent hours trying multiple approaches. Thanks. btw, can't vote up - new account. Sorry.

    – Imtiaz Ullah
    Nov 21 '18 at 16:37













  • @ImtiazUllah you should be able to use the checkmark beneath upvoting to accept an answer

    – Conner
    Nov 21 '18 at 16:40


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














import pandas as pd
pd.DataFrame(d['body']['stores']).to_csv('my_file.csv')





share|improve this answer
























  • Conner you legend! I can't believe it's just a line of code - I've spent hours trying multiple approaches. Thanks. btw, can't vote up - new account. Sorry.

    – Imtiaz Ullah
    Nov 21 '18 at 16:37













  • @ImtiazUllah you should be able to use the checkmark beneath upvoting to accept an answer

    – Conner
    Nov 21 '18 at 16:40
















0














import pandas as pd
pd.DataFrame(d['body']['stores']).to_csv('my_file.csv')





share|improve this answer
























  • Conner you legend! I can't believe it's just a line of code - I've spent hours trying multiple approaches. Thanks. btw, can't vote up - new account. Sorry.

    – Imtiaz Ullah
    Nov 21 '18 at 16:37













  • @ImtiazUllah you should be able to use the checkmark beneath upvoting to accept an answer

    – Conner
    Nov 21 '18 at 16:40














0












0








0







import pandas as pd
pd.DataFrame(d['body']['stores']).to_csv('my_file.csv')





share|improve this answer













import pandas as pd
pd.DataFrame(d['body']['stores']).to_csv('my_file.csv')






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 16:22









ConnerConner

23.7k84568




23.7k84568













  • Conner you legend! I can't believe it's just a line of code - I've spent hours trying multiple approaches. Thanks. btw, can't vote up - new account. Sorry.

    – Imtiaz Ullah
    Nov 21 '18 at 16:37













  • @ImtiazUllah you should be able to use the checkmark beneath upvoting to accept an answer

    – Conner
    Nov 21 '18 at 16:40



















  • Conner you legend! I can't believe it's just a line of code - I've spent hours trying multiple approaches. Thanks. btw, can't vote up - new account. Sorry.

    – Imtiaz Ullah
    Nov 21 '18 at 16:37













  • @ImtiazUllah you should be able to use the checkmark beneath upvoting to accept an answer

    – Conner
    Nov 21 '18 at 16:40

















Conner you legend! I can't believe it's just a line of code - I've spent hours trying multiple approaches. Thanks. btw, can't vote up - new account. Sorry.

– Imtiaz Ullah
Nov 21 '18 at 16:37







Conner you legend! I can't believe it's just a line of code - I've spent hours trying multiple approaches. Thanks. btw, can't vote up - new account. Sorry.

– Imtiaz Ullah
Nov 21 '18 at 16:37















@ImtiazUllah you should be able to use the checkmark beneath upvoting to accept an answer

– Conner
Nov 21 '18 at 16:40





@ImtiazUllah you should be able to use the checkmark beneath upvoting to accept an answer

– Conner
Nov 21 '18 at 16:40





Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)