A
A
artloveyou2021-12-20 21:25:11
Python
artloveyou, 2021-12-20 21:25:11

How to get object value from json?

{
    "0": {
        "bar": "test0"
    },
    "1": {
        "bar": "test1"
    },
    "2": {
        "bar": "test2"
    }
}



How to get test, test1, test2 from the above json
import json

with open('test.json') as json_file:
    data = json.load(json_file)

    for item in data:
        print(item['bar'])


neither does it work
import json

with open('test.json') as json_file:
    data = json.load(json_file)

    for item in data:
        for test in item:
            print(test)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-12-20
@artloveyou

import json

with open('test.json') as json_file:
    data = json.load(json_file)

    for item in data.values():
        print(item['bar'])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question