Working with JSON APIs
What is JSON?
JSON is a text format for structured data.
- objects โ Python dict
- arrays โ Python list
Parse JSON
parse_json.py
import requests
r = requests.get("https://api.github.com", timeout=10)
obj = r.json()
print(type(obj))
print(obj.get("current_user_url"))parse_json.py
import requests
r = requests.get("https://api.github.com", timeout=10)
obj = r.json()
print(type(obj))
print(obj.get("current_user_url"))Validate fields safely
APIs may change or fields may be missing.
safe_access.py
data = {"user": {"name": "Ravi"}}
name = data.get("user", {}).get("name")
email = data.get("user", {}).get("email") # might be None
print(name, email)safe_access.py
data = {"user": {"name": "Ravi"}}
name = data.get("user", {}).get("name")
email = data.get("user", {}).get("email") # might be None
print(name, email)Convert JSON list to pandas
json_to_df.py
import requests
import pandas as pd
url = "https://api.github.com/search/repositories"
params = {"q": "data analytics", "per_page": 10}
r = requests.get(url, params=params, timeout=10)
r.raise_for_status()
items = r.json()["items"]
rows = [
{
"name": it["full_name"],
"stars": it["stargazers_count"],
"language": it.get("language"),
}
for it in items
]
df = pd.DataFrame(rows)
print(df.head())json_to_df.py
import requests
import pandas as pd
url = "https://api.github.com/search/repositories"
params = {"q": "data analytics", "per_page": 10}
r = requests.get(url, params=params, timeout=10)
r.raise_for_status()
items = r.json()["items"]
rows = [
{
"name": it["full_name"],
"stars": it["stargazers_count"],
"language": it.get("language"),
}
for it in items
]
df = pd.DataFrame(rows)
print(df.head())Common errors
KeyErrorKeyError: field missing โ use.get().get()JSONDecodeErrorJSONDecodeError: response not JSON โ check status code/content
๐งช Try It Yourself
Exercise 1 โ Parse JSON Response
Exercise 2 โ Serialize Python to JSON
Exercise 3 โ Parse JSON String
If this helped you, consider buying me a coffee โ
Buy me a coffeeWas this page helpful?
Let us know how we did
