Accessing nested values in JSON data from Twitch API

Jaon

I am using the code:

getSource = requests.get("https://api.twitch.tv/kraken/streams")
text = json.loads(getSource.text)
print text["streams"]["_links"]["_self"]

text is a dictionary that I get by calling json.loads. I then try to get the nested values, eventually trying to get the url but this error shows up:

Traceback (most recent call last):
  File "Twitch Strim.py", line 11, in <module>
    print text["streams"]["_links"]["_self"]
TypeError: list indices must be integers, not str

How can I correct this?

Anshul Goyal

You are accessing the json data wrong, the correct way of accessing this would be:

print text["streams"][0]["_links"]["self"]

Full code:

>>> import requests
>>> import json
>>> getSource = requests.get("https://api.twitch.tv/kraken/streams")
>>> text = json.loads(getSource.text)
>>> print text["streams"][0]["_links"]["self"]
https://api.twitch.tv/kraken/streams/summit1g
>>> print text["streams"][1]["_links"]["self"]
https://api.twitch.tv/kraken/streams/tsm_theoddone
>>> print text["streams"][2]["_links"]["self"]
https://api.twitch.tv/kraken/streams/reynad27

Note that text["streams"] provides you a list of items (and not a dict), so you will have to access by its index values.

You can check the same using

>>> type(text["streams"])
<type 'list'>

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Accessing Json data through api with Python

分類Dev

Accessing data from Wikidata JSON file

分類Dev

JSON Data from api

分類Dev

How Web API Return Nested JSON Values by HttpPost call from database?

分類Dev

Extracting values from a nested JSON file

分類Dev

Accessing nested data in a supposed dict

分類Dev

Accessing a json api with php

分類Dev

Get nested data from local JSON file

分類Dev

Accessing multiple nested hiera values from puppet code (or "puppet lookup" cmd)

分類Dev

How to extract array values within nested JSON data

分類Dev

Get username from Twitch API using Authorization Code Flow (php)

分類Dev

How to display JSON values from API

分類Dev

Converting nested dict from json into dataframe with values as columns

分類Dev

Check nested Array values, if a match: combine data from both

分類Dev

Display random values from JSON data

分類Dev

Golang find a value from a map of nested json data from MongoDB

分類Dev

Accessing Cosmos data from widget

分類Dev

How to display the nested array data in react native from API?

分類Dev

Vue CLI - Parsing nested data in component from local JSON

分類Dev

Extracting data from very nested JSON in Azure SQL

分類Dev

Can't decode JSON data from API

分類Dev

Golang - parsing nested json values

分類Dev

Read json values with nested objects

分類Dev

Accessing only one of the return values from a function

分類Dev

Issue in accessing API/json with multiple sections

分類Dev

Accessing Laravel Rest API with curl - JSON

分類Dev

Twitch API redirect_mismatch

分類Dev

Accessing nested JSON Value with variable key name in Logstash

分類Dev

Accessing nested array in JSON response. Typescript-ReactJS

Related 関連記事

ホットタグ

アーカイブ