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
Martin MillerCTOAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Must be valid JSON to start.

Nested JSON works, so long as the JSON validates. 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 MillerCTOAuthor Commented:
@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 BradyPrincipal Data EngineerCommented:
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', '{', '['

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!
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
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 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 MillerCTOAuthor Commented:
I have uploaded a valid json, my question is still the same... how to parse the nested elements. Thanks!!!
Mark BradyPrincipal Data EngineerCommented:
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.
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
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 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 MillerCTOAuthor Commented:
@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']))

Open in new window

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

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

thanks again!!!

David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Best to post a copy of your valid JSON + likely someone can give you the exact syntax required to access data you're after.
Martin MillerCTOAuthor Commented:
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 ?

Mark BradyPrincipal Data EngineerCommented:
productBarcodes is a list of dicts. Depending on if you just want the value of barcodeType or you want to loop through multiple values in that array will be how to do it. To get the individual value:

To loop multiple dicts:

for this_obj in obj['productBarcodes']:
    print(this_obj['barcodeType']) + '\n'

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin MillerCTOAuthor Commented:
Thanks Mark, Very much appreciated!
Mark BradyPrincipal Data EngineerCommented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.