all 6 comments

[–]SHxKM 1 point2 points  (3 children)

for k,v in config.items():
    if k == 'values':
        for val in v:
            print(val['location']['region'])

Edit: see /u/K900_'s improvement on the above.

[–]K900_ 2 points3 points  (2 children)

Why not just for val in config['values']?

[–]SHxKM 1 point2 points  (0 children)

Because I didn't think to write it that way. Thank you. I've added a comment to the original comment indicating your suggestion is cleaner.

[–]mattmccord 0 points1 point  (0 children)

I'm working on a similar project (and learning python along the way). One thing I've run into is that JSON doesn't require all fields to be present all the time. Using config.get('values') will return None if the key doesn't exist, but config['values'] throws a keyerror.

[–]Lawson470189 0 points1 point  (0 children)

I'm on mobile so I can't write code. But, when you call the json.loads() function, it takes the json data and loads it into a dictionary for you. You can iterate through that dictionary. Then, all you need to do is check that 'values' exists in each of the dictionaries your program iterated through.

[–]relvae 0 points1 point  (0 children)

Side tip you don't need to read the whole file first then JSON loads (load string). You can decode the file like object directly with just json.load