Andrew Miotke
Posting about IT stuff. Sometimes programming, usually Python or Swift.
Parsing JSON using the Requests Python library
The Requests library can parse out JSON key:value pairs. It’s fairly simple so lets go through it.
Make the GET request
This does assume that Requests is already installed using pip install requests.
- Import
requests. - Put your URL into a variable. This just makes calling it more readable later on.
- Set up the following Python dicts:
payloadandheaders.
Example:
import requests
url = "https://api.github.com/repos/octocat/Hello-World"
payload = {}
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
# Not using an API key in this example due to a public API endpoint.
#"Authorization": f"{API_KEY}"
}
Note: If the API you’re trying to talk to requires an API key (most do) then use "Authorization": "API_KEY".
Note 2: NEVER put the actual API key in code, call it from an environment variable or other secure method.
Call the API
- Next we’ll make a new variable and store the returned value of
requets.get()passing in theurl,headers, andpayloadvariables we created earlier. - We’ll put this response into a new variable as JSON.
- We can print the response to get a blob of JSON shown in example 2 of this section.
Example 1:
# 1
response = requests.get(url, headers=headers, data=payload)
# 2
response_data = response.json()
# 3
print(response_data)
Example 2 (truncated JSON):
{'id': 1296269, 'node_id': 'MDEwOlJlcG9zaXRvcnkxMjk2MjY5', 'name': 'Hello-World'},
Parsing the JSON
This part is fairly simple but can be more complex as the JSON becomes more complex. This is a very straight forward example.
We’ll be parsing the JSON from the URL (and in Example 2 above) to get the name of a repo.
- Create a new variable, maybe named after the JSON key we’re getting the value for. Use
.get("")on theresponse_datavariable and pass in thekeyfor thevaluethat you want. Remember, this is looking for akey:valuepair. - Print the value.
This outputs the name of the repo Hello-World. We put the output in an f-string just to fancy it up.
Example:
# 1
repo_name = response_data.get("name")
#2
print(f"Repo name: {repo_name}")
# Output: Repo name: Hello-World
Full code example
import requests
def main():
url = "https://api.github.com/repos/octocat/Hello-World"
payload = {}
headers = {
"Accept": "application/json",
"Content-Type": "application/json",
# Not using an API key in this example due to a public API endpoint.
#"Authorization": f"{API_KEY}"
}
response = requests.get(url, headers=headers, data=payload)
response_data = response.json()
repo_name = response_data.get("name")
print(f"Repo name: {repo_name}")
if __name__ == "__main__":
main()