el323
el323

Reputation: 2920

How to parse json values from API response

I am trying to parse json response from an API.

response = requests.post('https://analysis.lastline.com/analysis/get_completed', files=files)

my = response.json()
print my

Output:

{u'data': {u'tasks': [], u'more_results_available': 0, u'after': u'2018-03-18 22:00:20', u'before': u'2018-03-18 17:00:22'}, u'success': 1}

Here my is a dictionary. Now I want to get values against the keys.

I have tried this:

print my['tasks']

It gives me KeyError.

Upvotes: 0

Views: 88

Answers (2)

MSS
MSS

Reputation: 3633

You have a nested dictionary. To access value against 'task' key, you should write like this:

print my['data']['tasks']

Upvotes: 1

Rakesh
Rakesh

Reputation: 82765

You need use the data key to access tasks

Ex:

d = {u'data': {u'tasks': [], u'more_results_available': 0, u'after': u'2018-03-18 22:00:20', u'before': u'2018-03-18 17:00:22'}, u'success': 1}
print(d["data"]["tasks"])
print(d["data"]["after"])

Output:

[]
2018-03-18 22:00:20

Upvotes: 1

Related Questions