shamirs888 shamirs888 - 2 months ago 9
JSON Question

Extracting specific values from specific keys from JSON in Python

so after searching in the website I didn't found a solution to my problem,
I would like to extract specific values from a Jason object to a dedicated list. my code looks like that:

url2 = 'https://pqrsiem/api/reference_data/sets/clearsky_DOMAIN?fields=data(first_seen)'

request = urllib2.Request(url2,headers=headers)

response = urllib2.urlopen(request)

parsed_response = json.loads(response.read().decode('utf-8'))

print(json.dumps(parsed_response, indent=4))


the output looks like this:

{
"data": [
{
"first_seen": 1474468912626
},
{
"first_seen": 1474468912694
},
{
"first_seen": 1474468912762
},
{
"first_seen": 1474468912831
},
{
"first_seen": 1474468912899
},
{
"first_seen": 1474468912962
},
{
"first_seen": 1474468913022
},
{
"first_seen": 1474468913081
},
{
"first_seen": 1474468913143
},
{
"first_seen": 1474468913203
},
{
"first_seen": 1474468913264
},
{
"first_seen": 1474468913327
},
{
"first_seen": 1474468913388
},
{
"first_seen": 1474468913450
},
{
"first_seen": 1474468913548
},
{
"first_seen": 1474468913626
},
{
"first_seen": 1474468913686
},
{
"first_seen": 1474468913745
},
{
"first_seen": 1474468913806
},
{
"first_seen": 1474468913867
},
{
"first_seen": 1474468913931
},
{
"first_seen": 1474468913992
},
{
"first_seen": 1474468914054
},
{
"first_seen": 1474468914117
},
{
"first_seen": 1474468914178
},
{
"first_seen": 1474468914239
},
{
"first_seen": 1474468914298
},
{
"first_seen": 1474468914357
}
]


}

I would like to extract the values from the "first_seen" key
how can I do it????

jmv jmv
Answer

if you have your json stored in parsed_response:

for i in range (len(parsed_response["data"])):
    print json["data"][i]["first_seen"]
Comments