Python Forum

Full Version: Parse JSON multiple objects
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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!
Don't use the quote button to post your code - see BBCode to know more about proper code tags usage.
Sorry about that, corrected below:

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"])
Error resulting from original JSON file:

Error:
TypeError: list indices must be integers or slices, not str
I fixed your tags in the original post. In the second post your indentation is wrong.
Also these print on line 4 and 10 will cause error. If they don't - you are using python2 and you should be using python3

Back to original post - you are overdoing things
1. Read json - get list of dicts
2.iterate over the list and parse each dict

import json

with open('party.json') as f:
    data = json.load(f)
for trade in data:
    print(trade["id"], trade["name"])
Thank you, buran. I'm on Python 3.7.6 with Spyder and am not receiving errors as indicated. I used your example and am returning
Error:
JSONDecodeError: Extra data
check your json file, there is something extra
with the example data in first post it works
see: https://repl.it/repls/SmoggyBlueState
if you are unsure of the validity of the json data then copy paste it into a online validator first, then you can least start knowing the data is kosher
print(type(data))
will tell you which type of variable is data
string, integer, json etc..
@pythonlearner1 - yes type will tell you just that, the type of the "data", but that will not tell you if the contents of the data is valid (and hence my suggestion to use a json validator if unsure first), in the case of the original posting, the variables the developer used can be tested as you say, but when the json he is reading is dumped into that variable it will just tell him the variable is of type "dict" or "list" or "str" for example, it wont tell him that the json is valid json necessarily, or that its presentation is without escape characters (encoding) depending on the method chosen or the package used - (there are a plethora of json parsers and validator packages in python), also the user was trying to iterate over a list in his second piece of code using a "dictionary" style "keywords", whereas a List requires indexes or an index range for splicing.