0

I want to loop through an arbitrary JSON object in Python and get all values. I do not know what the JSON object looks like. I just want to loop through all elements. So also through the child objects and lists. I have now taken an arbitrary JSON object from the Internet to test. But it could also be any other JSON object. Currently I try it like this:

def printJSONVals(json_object): 
 it = iter(json_object)
 n = json_object[next(it)]
 while n is not None:
 print n
 n = json_object[next(it)]
json_obj = json.loads('{ "city" : "AGAWAM", "loc" : [ -72.622739, 42.070206 ], "pop" : 15338, "state" : "MA", "_id" : "01001"}')
printJSONVals(json_obj)

Unfortunately, this does not work, because I get the list "loc" back as a list and not the individual values. Is there a way to loop through a JSON object regardless of the depth of the nesting and always store the value of the individual keys in the variable n?

As a result, I want the variable n to always contain the values of the keys. Regardless of the depth of the nesting of the object. So I expect this output here:

AGAWAM
-72.622739
42.070206
15338
MA
01001

I would like to write these later in a two-dimensional array. But this is not important. It is enough if I manage to output the values independent of the nesting.

I use Python version 2.7.17

Thank you

asked Feb 9, 2020 at 18:42
13
  • What problem are you actually trying to solve? Commented Feb 9, 2020 at 18:45
  • I want to create a two-dimensional array, which always contains the following data: ("key", "parent", "val") Commented Feb 9, 2020 at 18:50
  • 1
    What do you mean by "parent"? Commented Feb 9, 2020 at 18:52
  • 1
    meta.stackoverflow.com/questions/253162/… Commented Feb 9, 2020 at 18:55
  • 1
    json_obj is not a "JSON object". It's a dict. JSON stopped being relevant as soon as json.loads returned. Commented Feb 9, 2020 at 19:50

1 Answer 1

1

Well, you can use dict keys to calculate the path of ancestors.

import json
def print_json(obj, parent=None):
 if isinstance(obj, list):
 for child in obj:
 for path, item in print_json(child, parent):
 yield path, item
 elif isinstance(obj, dict):
 for key, child in obj.iteritems(): # items() in Python3
 child_path = [key] if parent is None else parent + [key]
 for path, item in print_json(child, child_path):
 yield path, item
 else:
 yield parent, obj

With you example, you have:

json_obj = json.loads(
 '{ "city" : "AGAWAM", "loc" : [ -72.622739, 42.070206 ], "pop" : 15338, "state" : "MA", "_id" : "01001"}'
)
for path, item in print_json(json_obj):
 print(path, ":", item)

The result:

['city'] : AGAWAM
['loc'] : -72.622739
['loc'] : 42.070206
['pop'] : 15338
['state'] : MA
['_id'] : 01001

Another example:

json_obj = json.loads(
 '{ "city" : "AGAWAM", "loc" : { "type": "int", "value": 16}}'
)

You get:

['city'] : AGAWAM
['loc', 'type'] : int
['loc', 'value'] : 16
answered Feb 9, 2020 at 19:10
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.