What is JSON?

If you have any experience programming for the web, you’ve probably seen or heard the term JSON. JSON stands for JavaScript Object Notation and it’s a text format used for storing and transporting data across the web.

You commonly encounter it when consuming APIs or when setting up configuration files. It’s very lightweight and language-independent. Many programming languages and frameworks provide their own JSON parsers to make working with JSON fast and easy.

JSON has often been compared with XML. The two technologies are both used to store data in text format but JSON is preferred when transferring client/server data on the web due to its smaller file size.

How do you pronounce JSON?

As with many acronyms found within the technology community, there is some debate over how to “correctly” pronounce JSON. Do we say “jason”, as in “Jason” Bateman, or do we say “jay-sahn” and draw out the second syllable? Well, let’s ask the guy who invented JSON, Douglas Crockford.

In a 2022 video posted on YouTube, Douglas briefly describes how he arrived at the term JSON and offers his take on the pronunciation: “jason”.

The JSON Syntax

JSON syntax provides a standard way to represent data using key-value pairs. The format is based on JavaScript’s object notation model and can be used to represent the following types of data:

  • Strings
  • Numbers
  • Booleans
  • Arrays
  • Objects
  • null

An example of JSON is shows below where flavor is a string, price is a number, isVegan is a boolean, and ingredients is an array of strings.

{
  "flavor": "Vanilla",
  "price": 10,
  "isVegan": false,
  "ingredients": ["milk", "cream", "eggs", "sugar", "vanilla"]
}

JSON files will often contain one or more objects. In the previous example, a single JSON object is displayed. In the example below, 3 objects are stored in an array.

[{
  "flavor": "Vanilla",
  "price": 10,
  "isVegan": false,
  "ingredients": ["milk", "cream", "eggs", "sugar", "vanilla"]
},
{
  "flavor": "Chocolate",
  "price": 12,
  "isVegan": true,
  "ingredients": ["water", "coconut milk", "cane sugar", "cocoa powder", "sea salt", "vegan dark chocolate", "vanilla extract"]
},
{
  "flavor": "Orange",
  "price": 14,
  "isVegan": false,
  "ingredients": ["cream", "orange juice", "lemon juice", "sugar", "vanilla extract"]
}]

Prettify JSON

When a web server responds with JSON, the data is usually minified. All the white space and line breaks are removed to reduce the file size as much as possible.

Expect to see is something like the following:

[{"flavor":"Vanilla","price":10,"isVegan":false,"ingredients":["milk","cream","eggs","sugar","vanilla"]},{"flavor":"Chocolate","price":12,"isVegan":true,"ingredients":["water","coconut milk","cane sugar","cocoa powder","sea salt","vegan dark chocolate","vanilla extract"]},{"flavor":"Orange","price":14,"isVegan":false,"ingredients":["cream","orange juice","lemon juice","sugar","vanilla extract"]}]

If you’re building an application that pulls data from a web API, you sometimes want to format the JSON response to make it easier to see what data you have to work with, rather than scrolling across long responses.

There are tools online that allow you to copy the JSON response over and reformat it into a more human-friendly version. Below are a couple options.

Examples

To demonstrate how JSON is used, let’s read a JSON file in Python. Python includes a json package that does a lot of the parsing for us. So, this will go quick.

In a project folder, create a file called icecream.json and paste in the code below:

[{
  "flavor": "Vanilla",
  "price": 10,
  "isVegan": false,
  "ingredients": ["milk", "cream", "eggs", "sugar", "vanilla"]
},
{
  "flavor": "Chocolate",
  "price": 12,
  "isVegan": true,
  "ingredients": ["water", "coconut milk", "cane sugar", "cocoa powder", "sea salt", "vegan dark chocolate", "vanilla extract"]
},
{
  "flavor": "Orange",
  "price": 14,
  "isVegan": false,
  "ingredients": ["cream", "orange juice", "lemon juice", "sugar", "vanilla extract"]
}]

Create another file called main.py and add the following:

import json

with open('icecream.json') as f:
  data = json.load(f)

  for icecream in data:
    print(icecream["flavor"])

The first line imports Python’s json package. Then we open the file and use json’s load method to return the contents as a list of dictionaries, which we assign to the variable data. Finally, we iterate over the list of dictionaries and print each ice cream flavor.

Vanilla
Chocolate
Orange

Working with JSON and Python is quick and painless. However, most JSON you encounter will likely come from an API. Let’s try another quick example in Python.

There is a free API called icanhazdadjoke that provides access to the largest selection of dad jokes on the internet. Making a GET request to their API returns a simple JSON response containing and ID, the joke, and a status code.

Below is a sample response from icanhazdadjoke.

{
  "id": "R7UfaahVfFd",
  "joke": "My dog used to chase people on a bike a lot. It got so bad I had to take his bike away.",
  "status": 200
}

Let’s use Python to make a GET request to the icanhazdadjoke API and retrieve a random dad joke.

Create a new project or update the previous main.py to match the following. Install the requests package if needed.

from requests import get

response = get("https://icanhazdadjoke.com/", headers={"Accept":"application/json"})
data = response.json()

print(data["joke"])

We pass the URL and header parameters into the requests' get function. The API returns JSON data but the requests module stores it as a requests.models.Response object, which stores additional information about the request such as the HTTP method, url, headers, params, cookies, and other metadata.

We use the requests' json() method to return the json-encoded content of the response and assign it to the variable data. Finally, we print out the joke.

Gonorrhea would have been a great name for diarrhea medicine.

The jokes are random. Your output will be different.

Summary

In this post we explored what JSON is and how Python can be used to read it.

JSON is one of the primary technologies APIs use to pass data between client and server so becoming familiar with JSON and how your preferred programming language handles it is a must for any programmer.