Read from requests.get

Question:

i have this request but its with a strange behaviour and im having dificulties get the values a need.

heres an example:

response = requests.get('https://api-prd.dev/analytics/api/properties?filter[a.id]=' + aid, headers=headers)
request = response.json()
print(":0:->>>> ", request['data'][0])

the prints are like this:

:0:->>>>  {'type': 'id-snapshot-property', 'id': 'adblue_level_percentage', 'attributes': {'updateDate': '2022-10-07T13:39:32Z', 'value': '0'}}

How do i get the value 0 in adblue_level_percentage

response: request['data'][0]['attributes']['value']

Asked By: pyrazs

||

Answers:

import json
response = requests.get('https://api-prd.dev/analytics/api/properties?filter[a.id]=' + aid, headers=headers)

request = json.loads(response)

now you have dict object and fetch value by calling dict key

Answered By: Ahmed Salman
response = requests.get('https://api-prd.dev/analytics/api/properties?filter[a.id]=' + aid, headers=headers)

data = response.json()

This should also work. It also delivers a dictionary which you can access via its keys.

Answered By: chrisaramar

Just use Response.json():

>>> import requests
>>> r = requests.get('https://api.github.com/events')
>>> r.json()
[{'repository': {'open_issues': 0, 'url': 'https://github.com/...'}}]
Answered By: Klas Š.
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.