Zzrot Zzrot - 2 months ago 18
JSON Question

parsing a large json file and retrieving values in python

I have one large json file of the format -

{
"x": "",
"y": {
"a": {
"-3": {
"id": -2,
"rut": "abc",

},
"-1": {
"id": -1,
"rut": "cdf",

}
}
}
}


Now I want to retrieve the values of
id
for all situations.
For this I have the following code -

import json
from pprint import pprint

with open('file.json') as data_file:
data = json.load(data_file)
data['y']['a'].value()['id']


Since I'm not too familiar with using json in python, I couldn't figure out what I was doing wrong. I used
.value()
because the values
-3
and
-1
could be any number and are not known before hand. The rest of the values are constant.

Answer

import json from pprint import pprint

with open('file.json') as data_file:
    data = json.load(data_file)
    pprint([item['id'] for item in data['y']['a'].values()])

Is it what you are looking for?