Python Basics

Read and Write JSON in Python

Turn JSON from an API into a Python dictionary and back again with two functions: json.loads and json.dumps.

  • Beginner
  • 2 min read
  • 1 runnable example
What you'll learn
  • json.loads and json.dumps
  • json.load and json.dump with files
  • How JSON types map to Python types
JSON → Python
{ "name": "Ada" }dict {'name': 'Ada'}
[1, 2, 3]list [1, 2, 3]
true / nullTrue / None
json.dumps goes the other way.

JSON text ⇄ Python values

The problem

Programs constantly exchange data: a web API sends a user, a settings file stores options, another program expects your results. They need a shared text format that both sides understand. JSON is that format, and Python must turn it into dictionaries and lists and back.

Think of it like this

JSON is like a form written in a standard layout that every office can read. json.loads reads the written form into Python data you can work with. json.dumps writes your Python data out again in the same standard layout, so any other program can read it.

From a standard form to code
json.loads(...)📥 Read the written form into Python data
user["score"] = 95🖊️ Add a field
json.dumps(user, indent=2)📤 Write it out again in the standard layout
true🔤 JSON's true becomes Python's True
JSON text in, Python values to work with, JSON text out.
In one sentence

JSON is a text format for data; the json module converts JSON text into Python values (json.loads) and Python values into JSON text (json.dumps).

Key points

  1. json.loads(...) turns the JSON text into a Python dictionary, with JSON true becoming True.
  2. user["score"] = 95 changes the data like any other dictionary.
  3. json.dumps(user, indent=2) writes it back as JSON text, indented for people to read.
  4. In the output, Python's True is written as JSON true again.

When should you use it?

  • Reading responses from web APIs and sending data to them.
  • Saving settings or small data sets in a readable file.
  • Passing data between programs written in different languages.

When not to use it

  • Dates, sets or your own classes: JSON cannot store them directly, so convert them to strings or dictionaries first.
  • Huge tables of numbers: CSV or a database is smaller and faster.

Example code

Run code →
main.py
import json

user = json.loads('{"name": "Ada", "active": true}')
user["score"] = 95

print(json.dumps(user, indent=2))
Get a Run button for this example

Result of running the code

{
  "name": "Ada",
  "active": true,
  "score": 95
}

Code explanation

Loads parses a JSON string. Dumps serializes the updated dictionary with indentation for readable output.

Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.

Practice exercises

Modify the runnable example with the exercises below to build understanding beyond copying the result.

  1. Change the input and predict the output before running.
  2. Handle empty or invalid input.
  3. Wrap the logic in a function and add more test cases.
Run in the Python compiler →

Common mistakes

Mixing up loads and load

loads reads a string; load reads from an open file. The s stands for string.

Problem
with open("user.json") as f:
    user = json.loads(f)
Fix
with open("user.json") as f:
    user = json.load(f)

Writing JSON with single quotes

JSON requires double quotes around keys and strings.

Problem
json.loads("{'name': 'Ada'}")
Fix
json.loads('{"name": "Ada"}')

Finished this lesson?

Mark it as learned to track your progress.

Practice with a challenge

Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.

All challenges