Requests library can parse out JSON key:value
pairs. It’s fairly simple so lets go through it.
This does assume that Requests is already installed using pip install requests
.
requests
.payload
and headers
.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.
requets.get()
passing in the url
, headers
, and payload
variables we created earlier.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'},
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.
.get("")
on the response_data
variable and pass in the key
for the value
that you want. Remember, this is looking for a key:value
pair.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
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()