skip to Main Content

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


  1. Just convert your text into a python dictionary using response.json(), in the example below:

    data = response.json()
    

    Then, loop over the data list inside the dictionary to print those IDs.

    for item in data['data']:
        print(item['id'])
    

    This should give you the result you want.

    Login or Signup to reply.
  2. You can use a nice list comprehension for this:

    output = [entry['id'] for entry in response.json()['data']]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search