Day 24 45 min beginner

Working with JSON

Learn how to store and exchange data using JSON, the language of the web.

Learning Objectives

  • Understand what JSON is and why it's used
  • Parse JSON strings into Python dictionaries
  • Convert Python data into JSON format
  • Save and load JSON data from files

The Language of Data

In Day 13, we learned to save text files. But text files can be messy. How do we save complex data like a user profile? We use JSON (JavaScript Object Notation).

JSON is the standard format for exchanging data between applications and the web. It looks almost exactly like a Python dictionary!

The json Module

Python has a built-in module called json to handle this format.

json_parse.py
1
2
3
4
5
6
7
8
9
import json

# A JSON string (imagine this came from a website)
user_json = '{"name": "Alice", "age": 25, "is_student": true}'

# Parse JSON into a Python Dictionary (Loads)
user_dict = json.loads(user_json)

print(user_dict["name"]) # Alice

Python to JSON (Dumps)

When you want to send data or save it, you convert your dictionary back to a JSON string.

json_generate.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import json

data = {
    "title": "Inception",
    "year": 2010,
    "actors": ["Leo", "Tom", "Elliot"]
}

# Convert to JSON string (Dumps)
json_string = json.dumps(data, indent=4) # indent makes it pretty!
print(json_string)

Saving to a File

Instead of strings, we usually work with actual files.

json_files.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import json

# Saving (Dump)
with open("settings.json", "w") as f:
    json.dump(data, f)

# Loading (Load)
with open("settings.json", "r") as f:
    loaded_data = json.load(f)
    print(loaded_data)

Interactive Practice

Compare the syntax between Python and JSON. Note how Python’s True becomes true in JSON.

Feature Python JSON
Boolean True / False true / false
Empty Value None null
Dictionary {"key": "value"} {"key": "value"}
List [1, 2, 3] [1, 2, 3]

Quiz

Complete this quiz with a minimum score of 80% to mark Day 24 as complete.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!