-1

I have the following API call...

response = requests.get(address, headers=headers, params=params)

I get a successful return and can see the data when I..

print(response.text)

The documentation says...

The API returns an array of stream objects, along with a cursor for pagination.

It gives an example response.

{
  "data": [
    {
      "id": "28623425344",
      "user_id": "150573462",
      "game_id": "488191",
      "community_ids": [
        "848d95be-90b3-44a5-b143-6e373754c382",
        "8deb7b0f-d5a3-4f9d-942d-2331d8f4fe3d",
        "ff1e77af-551d-4993-945c-f8ceaa2a2829"
      ],
      "type": "live",
      "title": "CHILL // PSYCHEDELIA :: ambience \u0026 tones  [ !visuals ]",
      "viewer_count": 9001,
      "started_at": "2018-05-08T18:47:34Z",
      "language": "en",
      "thumbnail_url": "https://static-cdn.jtvnw.net/previews-ttv/live_user_amorelandra-{width}x{height}.jpg"
    }
    ...
  ],
  "pagination": {
    "cursor": "eyJiIjpudWxsLCJhIjp7Ik9mZnNldCI6MX19"
  }
}

I don't know how to access for example, user_id from the response variable.

I'm new to Python.

I tried

print(response.data[0].user_id) but I get

AttributeError: 'Response' object has no attribute 'data'
deanresin
  • 1,466
  • 2
  • 16
  • 31

1 Answers1

1

You can convert the response string into a Python dictionary with the json library. Try this:

from json import loads
loads(response.text)['data'][0]['user_id']

Hope that helps.

Dallas Lindauer
  • 246
  • 1
  • 4