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

鏡平學校

ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

Why https connections are so slow when debugging (stepping over) in Java?