Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parse JSON multiple objects
#1
I'm having trouble parsing multiple objects within a JSON array. I can get my code to work, but I have to manipulate the JSON file which I shouldn't have to do.

I'm on Python 3, and here's my code:

import json

tradingList = []
print
with open('party.json') as f:
    for jsonObj in f:
        tradingDict = json.loads(jsonObj)
        tradingList.append(tradingDict)

print
for trade in tradingList:
    print(trade["id"], trade["name"])
Here's my original JSON data:

Output:
[{"id":3090,"name":"Wegmans","aventionId": null}, {"id":1156,"name":"Giant","aventionId": null}, {"id":4340,"name":"Safeway","aventionId": null}, {"id":5965,"name":"Publix","aventionId":[]}]
That results in an error "list indices ust be integers or slices, not str".

If I delete the square brackets (array), delete the commas, insert hard returns after each line the data comes back correct. Here's how I reformat the JSON:
Output:
{"id":3090,"name":"Wegmans","aventionId": null} {"id":1156,"name":"Giant","aventionId": null} {"id":4340,"name":"Safeway","aventionId": null} {"id":5965,"name":"Publix","aventionId":[]}
Here's the desired output:

Output:
3090 Wegmans 1156 Giant 4340 Safeway 5965 Publix
Any help is appreciate, thank you!
Reply


Messages In This Thread
Parse JSON multiple objects - by larkin_L - May-26-2020, 12:54 PM
RE: Parse JSON multiple objects - by pyzyx3qwerty - May-26-2020, 01:04 PM
RE: Parse JSON multiple objects - by larkin_L - May-26-2020, 01:46 PM
RE: Parse JSON multiple objects - by buran - May-26-2020, 01:53 PM
RE: Parse JSON multiple objects - by larkin_L - May-26-2020, 02:03 PM
RE: Parse JSON multiple objects - by buran - May-26-2020, 02:15 PM
RE: Parse JSON multiple objects - by nuffink - May-26-2020, 02:27 PM
RE: Parse JSON multiple objects - by pythonlearner1 - May-26-2020, 08:37 PM
RE: Parse JSON multiple objects - by nuffink - May-27-2020, 11:18 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  parse json field from csv file lebossejames 4 772 Nov-14-2023, 11:34 PM
Last Post: snippsat
  [split] Parse Nested JSON String in Python mmm07 4 1,551 Mar-28-2023, 06:07 PM
Last Post: snippsat
  Trying to parse only 3 key values from json file cubangt 8 3,505 Jul-16-2022, 02:05 PM
Last Post: deanhystad
  geojson to json --missing multiple row output yoshi 9 2,807 Mar-06-2022, 08:34 PM
Last Post: snippsat
  Problem to parse a json enigma619 3 2,393 Dec-04-2020, 08:16 AM
Last Post: enigma619
  How to parse JSON DIC? ogautier 4 2,258 Sep-15-2020, 06:03 PM
Last Post: ogautier
  Looking for help in Parse multiple XMLs and update key node values and generate Out.. rajesh3383 0 1,884 Sep-15-2020, 01:42 PM
Last Post: rajesh3383
  How can i parse a log file to JSON. menarcarlos 2 2,445 May-26-2020, 10:23 AM
Last Post: buran
  print python json dump onto multiple lines lhailey 2 19,899 Mar-02-2020, 12:47 PM
Last Post: vishalhule
  How to serialize custom class objects in JSON? Exsul1 4 3,514 Sep-23-2019, 08:27 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020