0

I would like to extract specific values from a JSON 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
    }, 
    ...
    ]
}

I would like to extract the values from the "first_seen" key and put them in a list. How can I do it?

Georgy
  • 12,464
  • 7
  • 65
  • 73
shamirs888
  • 19
  • 1
  • 7
  • Does this answer your question? [Getting a list of values from a list of dicts](https://stackoverflow.com/questions/7271482/getting-a-list-of-values-from-a-list-of-dicts) – Georgy Jan 24 '20 at 10:44

2 Answers2

0

I think you can try something like this:

>>> import json
>>>
>>>
>>> json_data = '{"data": [{"first_seen": 1474468912626}, {"first_seen": 1474468912694}, {"first_seen": 1474468912762}]}'
>>> parsed_response = json.loads(json_data)
>>>
>>>
>>> for element in parsed_response['data']:
...     print element['first_seen']
...
1474468912626
1474468912694
1474468912762
Roman Mogylatov
  • 516
  • 4
  • 10
0

if you have your json stored in parsed_response:

for i in range (len(parsed_response["data"])):
    print json["data"][i]["first_seen"]
jmv
  • 16
  • 2