IntermediateLesson firstCategory 17 of 20

JSON Handling

Serialize and deserialize JSON data. Read the lesson first, then move through the exercises in order.

7 Sections5 Exercises

After reading

Practice Arena

Begin with the first exercise, then continue step by step through the module.

Start with Deserialization Configurations

Study Material

Read the full lesson

What JSON is

JSON is a simple text format for data. It looks like Python dictionaries and lists, but it is just text.

Converting JSON to Python (loads)

json.loads() takes a JSON string and returns Python data.

python
import json text = '{"name": "Lina", "age": 28}' data = json.loads(text) print(data["name"])

Converting Python to JSON (dumps)

json.dumps() turns Python data into a JSON string.

python
data = {"name": "Lina", "age": 28} text = json.dumps(data) print(text)

Reading JSON from a file (load)

python
with open("data.json", "r") as f: data = json.load(f)

Writing JSON to a file (dump)

python
with open("data.json", "w") as f: json.dump(data, f, indent=2)

Common mistakes to avoid

  • Using single quotes in JSON (JSON requires double quotes).
  • Forgetting that JSON only supports basic types (no tuples or sets).
  • Treating JSON as a Python dict without calling json.loads() or json.load().

What you should understand after this lesson

  • The difference between load(s) and dump(s).
  • How to read and write JSON files.
  • The basic limits of JSON types.

Interactive

Exercises for this topic

These exercises follow the exact order of the lesson. Move step-by-step from reading into coding.

1Exercise

Deserialization Configurations

Retrieve strings outputs checking configurations strings sequences structures parameters parsing inputs extracting objects constraints strings ranges.

Open exercise
2Exercise

Serialization Outputs Mapping

Transform limits objects properties identifying parameters identifying structures targets outputs states isolating features tracking vectors layers properties formatting loops subsets formats objects bounds properties variables.

Open exercise
3Exercise

Nested Values Sequences

Determine nested limits objects checking configurations limits parameters configuring tracking loops tracking features isolating objects tracking parameters vectors loops elements outputs variables checks objects parameters extracting subsets formatting values variables formatting arrays formats arrays formats lists checking constraints checking parameters navigating vectors subsets features bounds.

Open exercise
4Exercise

Data Exceptions Sequences

Capture invalid targets tracking parameters sequences navigating vectors checking limits constraints ranges verifying bounds executing constraints vectors parameters sequences loops arrays navigating variables inputs parameters.

Open exercise
5Exercise

Default Objects Tracking

Exploit configs mappings bounds tracking elements loops loops parsing bounds outputs formatting properties mapping formatting outputs tracking tracking variables sequences limits strings vectors sequences tracking vectors checking checking formats targets configurations properties variables arrays arrays arrays navigating bounds subsets inputs isolating parameters arrays checking tracking features tracking instances configs limits inputs navigating paths strings parsing objects tracking variables arrays targets subsets targets sequences tracking.

Open exercise