Skip main navigation

JavaScript Object Notation

Learn more about JavaScript Object Notation.(J.O.N).

JavaScript Object Notation or JSON is much more than a structured method to store data. JSON is the language that helps projects interact over the internet.

JSON is a lightweight data-interchange format that is easy for humans to read and write while remaining easy for machines to parse and generate. JSON is regularly used when working with application programming interfaces (APIs) such as those provided by Twitter, Facebook, and NASA, as it provides a standard format for those services to provide data to your programs.

Tip: Parsing is where data is split into its component parts for checking or other processing.

Creating a JSON data structure

JSON is remarkably similar to Python’s dictionary data type, in that it uses a key–value pairing to store data. To demonstrate this you will create a project that will store the details of the first person on the moon, Neil Armstrong, using a JSON structure.

 import json
 data = {
 "Name": "Neil Armstrong",
 "Age": 82,
 "Hobbies": ["Aircraft design", "Fishing", "Astronaut"]
 }

 

This program first imports the json library, enabling us to use JSON with Python. It then creates a dictionary called data storing three values. The first key is Name and the value it references is the name of astronaut Neil Armstrong, stored as a string. The next key is Age and the value stored here is Neil’s age at the time of his death, stored as an integer. The last key is Hobbies and the value for this is a list of his hobbies, each stored as a string.

 

 with open("neil.json", "w") as f:
 json.dump(data, f)

 

To create persistent data storage of this information you use the standard file open method, which will create a new writable file, neil.json, and the json.dump function, which will write the dictionary to that file in JSON format.

 

Run the code yourself, and then open neil.json using a text editor.The contents should look like this:

 

 {"Name": "Neil Armstrong", "Age": 82, "Hobbies": ["Aircraft design", "Fishing", "Astronaut"]}

 

Notice that the JSON syntax is identical to Python’s dictionary.

 

Try importing the JSON data back into the program using the following code:

 

 with open("neil.json", "r") as f:
 data2 = json.load(f)
 print(data2)

 

This opens the JSON file in read "r" mode, and uses the json.load method to read the JSON file.

 

You should see that data2 is a dictionary, the same as data. You can obtain the values from this dictionary by using the relevant keys, as you saw earlier this week.

 

You may have noticed that I have chosen three different data types to be stored in the JSON file and dictionary. JSON is much more flexible than CSV in that you can store many different data types, mixing integers, strings, and lists, and can even store another dictionary. Even with this complexity, JSON still provides an easy to read and write syntax that can be used to store complex data.

 

Using JSON with a public API

 

 

JSON is not just a format for storing data but one for transmitting data. As mentioned earlier, JSON is commonly used with APIs on the internet.

 

An example: Using the NASA API

 

NASA provides an API that can be used to extract scientific and astronomical data and images taken during various missions. NASA offers this data in JSON format for free via a public API and you can work with that data using Python. To work with data from the internet you can use the requests library, which allows you to send and receive data over a network connection.

 

import requests
r = requests.get("https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY")
NASA = r.json()
print(NASA)

 

This example uses the requests library to get data from the NASA API and store it into an object r. The data in r is then parsed from JSON data into a Python dictionary using r.json(). The entire dictionary structure is then printed to the REPL. In the structure of this dictionary you would see many keys, one of which is explanation — the associated value provides a text description of an astronomical image.

Note: Depending on your setup you may need to install the Python package requests to try this example. The Installing Python packages guide provides advice on how to install Python packages for the most typical scenarios.

JSON is an amazing tool for structuring data. Over the next few steps you will see how to use it to make a game with persistent structured storage.

Can you think of a project where it would be useful to store another dictionary inside a JSON file? Explain why in the comments section.

This article is from the free online

Programming 103: Saving and Structuring Data

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now