I am using an API for a software where i wanna get the ID of something. When i make the get request using this code a
import requests
import json
url = "apiurl"
payload = ""
headers = { 'Authorization': 'Bearer .eyJ', 'Content-Type': 'application/json'}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
I get this response
{'current_page': 5, 'data': [{'id': 374047524, 'userId': 1295246, 'teamId': 24112628, 'name':
and a lot more info
What i wanna get is only the ‘id’ part
So if i request for multiple api info not just a single one i just wanna see
output
374047524
374047525
374047526
I tried using some json filters and whatever but i cant make it work. Please help!
Explained already above.
2
Answers
Just convert your text into a python dictionary using
response.json()
, in the example below:Then, loop over the
data
list inside the dictionary to print those IDs.This should give you the result you want.
You can use a nice list comprehension for this:
output = [entry['id'] for entry in response.json()['data']]