4

I'm not sure how to loop through this particular structure where the top level keys A1 and B6 are random.

I want to be able to output name, x, y and yr

{
    "A1": {
        "msgs": ["Something there"],
        "name": "Mary White",
        "x": 132,
        "y": 73,
        "yr": 1978
    },
    "B6": {
        "msgs": ["Something here"],
        "name": "Joe Bloggs",
        "x": 132,
        "y": 73,
        "yr": 2016
    },
...

Using the following to load my JSON

import json

with open('items.json') as data_file:    
    data = json.load(data_file)
2
  • @ZdaR no, you'd need to loop through .items(). Commented Jan 3, 2017 at 14:22
  • Where does json_ come into play? Updated question Commented Jan 3, 2017 at 14:24

1 Answer 1

17

Iterate through .values():

import json

with open('data.json') as data_file:    
    data = json.load(data_file)
    for v in data.values():
        print(v['x'], v['y'], v['yr'])
Sign up to request clarification or add additional context in comments.

2 Comments

That only prints out the first one
@pee2pee Works fine for me. Check your json file. And try to print(data).

Your Answer

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.