JSON to CSV using Python [closed]
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
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.
add a comment |
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
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
add a comment |
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
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
python json csv
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
import pandas as pd
pd.DataFrame(d['body']['stores']).to_csv('my_file.csv')
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
import pandas as pd
pd.DataFrame(d['body']['stores']).to_csv('my_file.csv')
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
add a comment |
import pandas as pd
pd.DataFrame(d['body']['stores']).to_csv('my_file.csv')
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
add a comment |
import pandas as pd
pd.DataFrame(d['body']['stores']).to_csv('my_file.csv')
import pandas as pd
pd.DataFrame(d['body']['stores']).to_csv('my_file.csv')
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
add a comment |
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
add a comment |
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