Skip to content
Advertisement

Extract specific value from JSON with Python

I am trying to get all the websites from this json file

Unfortunately, when I use this code:

import requests

response = requests.get("https://github.com/solana-labs/token-list/blob/main/src/tokens/solana.tokenlist.json")
output = response.json()

# Extract specific node content.
print(output['website'])

I get following error:

Traceback (most recent call last):
File "/Users/dusandev/Desktop/StreamFlowWebTests/extract.py", line 5, in <module>
output = response.json()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site- 
packages/requests/models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File 
"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", 
line 346, in loads
return _default_decoder.decode(s)
File 
"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", 
line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
 File 
"/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", 
 line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 7 column 1 (char 6)

Any help is appreciated. Thank you in advance

Advertisement

Answer

Use raw data to get raw json and then iterate over ‘tokensattr of the response object:

import requests

response = requests.get(
    "https://raw.githubusercontent.com/solana-labs/token-list/main/src/tokens/solana.tokenlist.json")

output = response.json()
for i in output['tokens']:
    if i.get('extensions'):
        print(i.get('extensions').get('website'))
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement