Still new to Python and need a little help here. I've found some answers for iterating through a list of dictionaries but not for nested dictionaries in a list of dictionaries.
Here is the a rough structure of a single dictionary within the dictionary list
[{ 'a':'1',
'b':'2',
'c':'3',
'd':{ 'ab':'12',
'cd':'34',
'ef':'56'},
'e':'4',
'f':'etc...'
}]
dict_list = [{ 'a':'1', 'b':'2', 'c':'3', 'd':{ 'ab':'12','cd':'34', 'ef':'56'}, 'e':'4', 'f':'etc...'}, { 'a':'2', 'b':'3', 'c':'4', 'd':{ 'ab':'23','cd':'45', 'ef':'67'}, 'e':'5', 'f':'etcx2...'},{},........,{}]
That's more or less what I am looking at although there are some keys with lists as values instead of a dictionary but I don't think I need to worry about them right now although code that would catch those would be great.
Here is what I have so far which does a great job of iterating through the json and returning all the values for each 'high level' key.
import ujson as json
with open('test.json', 'r') as f:
json_text = f.read()
dict_list = json.loads(json_text)
for dic in dict_list:
for val in dic.values():
print(val)
Here is the first set of values that are returned when that loop runs
1
2
3
{'ab':'12','cd':'34','ef':'56'}
4
etc...
What I need to be able to do pick specific values from the top level and go one level deeper and grab specific values in that nested dictionary and append them to a list(s). I'm sure I am missing a simple solution. Maybe I'm looking at multiple loops?
Following the ducktype style encouraged with Python, just guess everything has a .values member, and catch it if they do not:
import ujson as json
with open('test.json', 'r') as f:
json_text = f.read()
dict_list = json.loads(json_text)
for dic in dict_list:
for val in dic.values():
try:
for l2_val in val.values():
print(l2_val)
except AttributeError:
print(val)
Bazingaa's solution would be faster if inner dictionaries are expected to be rare.
Of course, any more "deep" and you would need some recursion probably:
def print_dict(d):
for val in d.values():
try:
print_dict(val)
except AttributeError:
print(val)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With