Working with JSON
JSON is the universal language of data on the internet. Every web API you'll ever call, most config files, and countless data feeds use it. Python's json module converts between JSON text and native Python objects in one line each direction.
Learn Working with JSON in our free Python course — a beginner-friendly interactive lesson with runnable examples, a practice exercise and a quick recall.
Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
If you can work with dictionaries and lists, you already understand JSON — it's the same structures written as text.
JSON is just structured text. Here's a typical API response describing a user:
If that looks like a Python dictionary, that's because the structures are almost identical. The differences are small but important:
Two functions cover converting between JSON text and Python objects :
Notice the round trip: true in JSON became True in Python, and back to true when dumped. The module handles all the type translation for you.
When working with files instead of strings, drop the "s". These read from / write to an open file object directly:
By default dumps produces one long line — fine for machines, painful for humans. Add indent to format it nicely:
Use indent for config files and debugging output you want to read. Skip it (compact) for network payloads where size matters.
JSON only knows about basic types. Hand it a datetime , a set , or a custom object and you'll hit a TypeError :
This pattern — load JSON on start, modify in memory, save JSON on exit — powers countless small tools and CLI apps:
The same three steps appear everywhere: json.load to read state, normal Python to change it, json.dump to save. JSON is your app's memory between runs.
These lines should parse JSON text and print one field, but they're scrambled. Put them in order:
Import, define the JSON string, parse it into a dict with loads , then access the key. After parsing, data is a normal dict.
<class 'list'> — a JSON array always becomes a Python list.
{' '} — Python's True becomes JSON true (lowercase) and None becomes null .
10 — the JSON number 1 is parsed as a real Python int , so arithmetic works directly. (No need to convert from a string.)
You speak the language of the web now!
You can parse JSON with loads/load, produce it with dumps/dump, pretty-print it, navigate nested structures, and handle tricky types like datetime. This unlocks every web API on the internet.
🚀 Up next: HTTP Requests & APIs — use the requests library to actually fetch JSON from real services.
Practice quiz
What does JSON stand for?
- Java Standard Object Notation
- Joined Sequence Object Names
- JavaScript Object Notation
- JavaScript Ordered Nodes
Answer: JavaScript Object Notation. JSON = JavaScript Object Notation, a lightweight text format for data exchange.
A JSON object {...} becomes which Python type?
- dict
- list
- tuple
- set
Answer: dict. A JSON object maps to a Python dict; a JSON array maps to a list.
What does json.loads(text) do?
- Writes JSON to a file
- Converts a Python object into a JSON string
- Reads a JSON file
- Converts a JSON string into a Python object
Answer: Converts a JSON string into a Python object. loads ('load string') parses a JSON string into a Python object.
What does json.dumps(obj) return?
- A Python dict
- A JSON string
- None (writes to file)
- A list
Answer: A JSON string. dumps ('dump string') serializes a Python object into a JSON string.
What is the difference between json.dump and json.dumps?
- dump writes to a file; dumps returns a string
- No difference
- dumps writes to a file; dump returns a string
- dump is for reading
Answer: dump writes to a file; dumps returns a string. The 's' means string: dumps returns a string, while dump writes to a file object.
JSON true and null become which Python values?
- true and null
- 'true' and 'null'
- True and None
- 1 and 0
Answer: True and None. JSON true/false/null map to Python True/False/None.
What does json.dumps({'ok': True, 'n': None}) produce?
- {"ok": True, "n": None}
- {"ok": true, "n": null}
- {'ok': true, 'n': null}
- {"ok": 1, "n": 0}
Answer: {"ok": true, "n": null}. Python True/None become JSON true/null (lowercase) with double-quoted keys.
How do you pretty-print JSON with indentation?
- json.dumps(data, pretty=True)
- json.dumps(data, format=2)
- json.pretty(data)
- json.dumps(data, indent=2)
Answer: json.dumps(data, indent=2). Pass indent=2 (or 4) to json.dumps/dump to format with newlines and indentation.
Why does json.dumps fail on a datetime object?
- datetime is too large
- json only serializes basic types (dict, list, str, int, float, bool, None)
- datetime is private
- You must import datetime first
Answer: json only serializes basic types (dict, list, str, int, float, bool, None). json can't serialize datetime directly; convert it first (e.g. .isoformat()) or pass default=str.
Which exception does json.loads raise on invalid JSON text?
- ValueError only
- TypeError
- json.JSONDecodeError
- SyntaxError
Answer: json.JSONDecodeError. Malformed JSON raises json.JSONDecodeError, so wrap untrusted parsing in try/except.