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.
- json.loads and json.dumps
- json.load and json.dump with files
- How JSON types map to Python types
{ "name": "Ada" }dict {'name': 'Ada'}[1, 2, 3]list [1, 2, 3]true / nullTrue / NoneJSON 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.
json.loads(...)📥 Read the written form into Python datauser["score"] = 95🖊️ Add a fieldjson.dumps(user, indent=2)📤 Write it out again in the standard layouttrue🔤 JSON's true becomes Python's TrueJSON 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
json.loads(...)turns the JSON text into a Python dictionary, with JSONtruebecomingTrue.user["score"] = 95changes the data like any other dictionary.json.dumps(user, indent=2)writes it back as JSON text, indented for people to read.- In the output, Python's
Trueis written as JSONtrueagain.
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
import json
user = json.loads('{"name": "Ada", "active": true}')
user["score"] = 95
print(json.dumps(user, indent=2))
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.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Mixing up loads and load
loads reads a string; load reads from an open file. The s stands for string.
with open("user.json") as f:
user = json.loads(f)
with open("user.json") as f:
user = json.load(f)
Writing JSON with single quotes
JSON requires double quotes around keys and strings.
json.loads("{'name': 'Ada'}")
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.
- Build a JSON Object Turn keys and values into a JSON object string, escaping quotes and backslashes correctly. Easy
- Sum a Flat JSON Object Parse a flat JSON object whose values are integers and add up the values. Easy