I tried to save json data into a variable, but it gives me KeyError in Python. There is my code:
import json
import requests
r = requests.get("https://api.followrel.ga/api.php?id=1")
# some JSON:
x = r.text
# parse x:
y = json.loads(x)
print(y["name"])
The error that I get:
Traceback (most recent call last):
File "main.py", line 12, in <module>
print(y["name"])
KeyError: 'name'
And finally the request text:
{"status":"true","message":"Customer Details","customers":{"id":"1","username":"petyadev","email":"[email protected]","name":"Peter Till","bio":"Hell\u00f3. Petya vagyok, a Followrel app k\u00e9sz\u00edt\u0151je.","job":"Followrel","website":"https:\/\/www.followrel.ga","coin":"500"}}
I will be very happy if someone can help me
CodePudding user response:
name does not exist in the y dict its nested inside the customers dict try:
print(y["customers"]["name"])