data
should be an ordinary collection at this point, so you'd iterate over it the same way you'd iterate over any other list/dict/whatever. The fact that it came from load
doesn't incur any extra requirements on your part.
Here's an example that uses loads
, which is similar in principle:
import json
my_json_data = "[1,2,3]"
data = json.loads(my_json_data)
for item in data:
print(item)
Result:
1
2
3
Edit: if you're asking "how to I iterate over all the values in my data, including the ones contained inside deeply nested collections?", then you could do something like:
import json
my_json_data = """[
1,
{
"2": 3,
"4": [
"5",
"6",
"7"
]
},
8,
9
]"""
def recursive_iter(obj):
if isinstance(obj, dict):
for item in obj.values():
yield from recursive_iter(item)
elif any(isinstance(obj, t) for t in (list, tuple)):
for item in obj:
yield from recursive_iter(item)
else:
yield obj
data = json.loads(my_json_data)
for item in recursive_iter(data):
print(item)
Result:
1
5
6
7
3
8
9
Edit: You can also keep track of the keys needed to navigate to each value, by passing them through the recursive calls and adding new keys to the collection as you pass over them.
def recursive_iter(obj, keys=()):
if isinstance(obj, dict):
for k, v in obj.items():
yield from recursive_iter(v, keys + (k,))
elif any(isinstance(obj, t) for t in (list, tuple)):
for idx, item in enumerate(obj):
yield from recursive_iter(item, keys + (idx,))
else:
yield keys, obj
data = json.loads(my_json_data)
for keys, item in recursive_iter(data):
print(keys, item)
Result:
(0,) 1
(1, '2') 3
(1, '4', 0) 5
(1, '4', 1) 6
(1, '4', 2) 7
(2,) 8
(3,) 9