Contoh API Python Json
# In this example, I will be referring to an API that contains information about astronauts currently in space
import json
import urllib.request # <-- Necessary imports
url = "http://api.open-notify.org/astros.json" # <-- The URL of the API
response = urllib.request.urlopen(url) # <-- Receiving a response from the API in JSON form
result = json.loads(response.read()) # <-- JSON reads the API's response
number = result["number"] # Finding the number of people in space
print(f"There are currently {number} people in space")
astros = result["people"] # Finding more info about the people in space
for astro in astros:
print(f"{astro['name'].title()} is currently on this spacecraft: {astro['craft']}")
# The API's response is structured like a JSON file (example response below), and that is why this works
# {
# "message": "success",
# "number": 2,
# "people": [{"name": "Person A", "craft": "ISS"},
# {"name": "Person B", "craft": "ISS"}]
# }
aguy11