JSON format.

 The json library has two main methods:

  - dumps -- Takes in a Python object, and converts it to a string.
  - loads -- Takes a json string, and converts it to a Python object.

JSON Format.

You may have noticed that the content of the response earlier was a string. Strings are the way that we pass information back and forth to APIs, but it's hard to get the information we want out of them. How do we know how to decode the string that we get back and work with it in python?

Luckily, there's a format called JavaScript Object Notation (JSON). JSON is a way to encode data structures like lists and dictionaries to strings that ensures that they are easily readable by machines. JSON is the primary format in which data is passed back and forth to APIs.

Python has great JSON support, with the json library. We can both convert lists and dictionaries to JSON, and convert strings to lists and dictionaries. In the case of our ISS Pass data, it is a dictionary encoded as a string in JSON format.

Here is example:

# import the json library                     
import json
import requests

# obtain data
request = requests.get("http://api.open-notify.org/astros.json")
# make it in json representation
astros = request.json()

# print numbers of astros in space and they's names
print('Number of astros:', astros['number'])
for name in astros['people']:
    print('Name of astro:', name['name'])
    
As result we can see following output:

Number of astros: 3
Name of astro: Sergey Rizhikov
Name of astro: Shane Kimbrough
Name of astro: Thomas Pesquet