Avatar of Martin Miller
Martin Miller
Flag for United States of America asked on

Parsing nested json

Parsing nested json. I am looking for help to parsing the nested json elements.

I attached a json file I need to output all of the data fields, however some are nested and I don't know the HOW-TO output the nested values. Can you help ?

I also include the start a python script
parser.py
file.json
PythonJSON

Avatar of undefined
Last Comment
Mark Brady

8/22/2022 - Mon
David Favor

Must be valid JSON to start.

Nested JSON works, so long as the JSON validates.

https://jsonformatter.org/ is a great validator tool which also formats JSON into human readable form.

Reformat your JSON to pass validation + likely all will work well.

Tip: Be sure to check the return result of json.loads() + bail out with an error in your code, to know if the JSON parse has succeeded.
Martin Miller

ASKER
@David thank's for the pointer to the validator, I am able to output the elements shown in the script attached, it's the nested elements, that I am not sure how-to do this in python, and ideas ?
Mark Brady

The main issue as has been stated is the JSON is NOT a JSON. It is a good attempt to be one but not quite. This particular Json has a lot of issues starting on line 3.
"nonPersonalizedCardLine3" :
Error: Parse error on line 3:
...nalizedCardLine3" : ValuedCustomer,"pro
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['
ValuedCustomer,

What that means is the text ValuedCustomer needs to be double quoted:  "ValuedCustomer",

To be valid all values should be one of the following:

STRING, NUMBER, NULL, true, false, { or [

Once you go ahead and fix up the example file to be a proper Json then parsing it will be super easy!
Your help has saved me hundreds of hours of internet surfing.
fblack61
David Favor

Mark... restated my point better than I did... "The main issue as has been stated is the JSON is NOT a JSON."

Your JSON must be JSON (validates clean) before your JSON will parse.

You said, "it's the nested elements, that I am not sure how-to do this in python, and ideas?"

Yes. Simple.

1) First you can't fix this... ever... any way... in your parser.py script... well... unless you write some hand rolled (custom) JSON parser which attempts to fix broken JSON.

You'll take this option if you're independently wealthy - unlimited time + money.

2) Better to fix this in the code generating the JSON.

3) To fix your JSON code generator, first output the output the outer block of your JSON (no nested elements) + ensure your JSON validates, then do the same for every individual JSON element, then merge the both outer block + nested element generation + you should be good.
Martin Miller

ASKER
I have uploaded a valid json, my question is still the same... how to parse the nested elements. Thanks!!!
Mark Brady

Can you make the new valid JSON available to us? In order to give you an example of how to parse it we need to see what we are working with.
Looking at the original JSON posted in this question the key of "productLocale" has a value which is a list of objects - or a list of dicts when it is json.loads(). To iter through each key => value in the nested objects:

for key, val in obj['productLocale'].iter():
    print('{KEY} => {VAL}'.format(KEY = key, VAL = val)

If you are not sure if the values are objects themselves you could do an isinstance check on each value before parsing it. If it is an instance of a dict or list then you know to iter the values. If not then that the values like a string (or whatever type of data is in there).

If you post the fixed json and tell us exactly what you expect to get out of it or your purpose for reading the data then we can help further.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
David Favor

You said, "I have uploaded a valid json, my question is still the same... how to parse the nested elements. Thanks!!!"

Once your JSON is valid, then your parser.py file will work as it's written, so valid JSON will parse correctly.

Maybe what you're asking is how you access these elements in your parsed data.

If this is what you're asking, then refer to Mark's comment.

Sometimes the easiest way to figure this out is to run your data through JSFormatter above. Once you have pretty (human readable) output, you can work out your exact parsing based on Mark's comment.
Martin Miller

ASKER
@Mark, Using this version of the script, I pasted output.
========================
import json
obj = json.loads(open('./file.json').read())
print("stopApprovalInd: " + str(obj['stopApprovalInd']))
print("creatorId: " + str(obj['creatorId']))
print("productBarcodes: " + str(obj['productBarcodes']))
print("\n")

Open in new window


stopApprovalInd: True
creatorId: PDM4PDZ0PDNQBGWMVGMRVTK9DM
productBarcodes: [{u'barcodeType': u'3D-CA128'}]

If I just want to output barcodeType, how do I resolve to just that ?

thanks again!!!

Martin
David Favor

Best to post a copy of your valid JSON + likely someone can give you the exact syntax required to access data you're after.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Martin Miller

ASKER
I updated the attached json file  2 days ago. it is valid, looking for an expert on the extract of elements that are embedded. These don't seem to fit as an item in an array to iter() on.  

Repeat the simple case here.
productBarcodes: [{u'barcodeType': u'3D-CA128'}]

Open in new window


If I just want to output barcodeType, how do I resolve to just that ?

Thanks!
ASKER CERTIFIED SOLUTION
Mark Brady

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Martin Miller

ASKER
Thanks Mark, Very much appreciated!
Mark Brady

Anytime!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.