Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python json api example

# 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"}]
# }   
Comment

PREVIOUS NEXT
Code Example
Python :: How To Display A Background Image With Tkinter 
Python :: python pandas convert series to percent 
Python :: try except python not working 
Python :: django add middleware 
Python :: display data from database in django 
Python :: python input integer only 
Python :: combination of 1 2 3 4 5 python 
Python :: how call module in the same directory 
Python :: breadth first search python 
Python :: tuple plot python 
Python :: how to access dataframe row by datetime index 
Python :: python print 2 decimal places 
Python :: spotipy currently playing 
Python :: Remove empty strings from the list of strings 
Python :: python program to find ascii value of character 
Python :: excute a command using py in cmd 
Python :: turn off warning when import python 
Python :: if main python 
Python :: check if year is leap python 
Python :: correlation with specific columns 
Python :: redirect a post request django 
Python :: scipy.cluster.hierarchy 
Python :: python code to replace first value of txt file 
Python :: delay print in python 
Python :: python array input from user 
Python :: Game of Piles Version 2 
Python :: python append value to dictionary list 
Python :: timer 1hr 
Python :: print inline output in python 
Python :: Matplotlib rotated x tick labels 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =