JSON is a text format used to exchange structured data between programs, APIs, files, and web apps.
Python uses the json module to convert JSON text into dictionaries and lists, then convert Python data back into JSON text.
Treat JSON as an exchange format: parse it at the boundary, work with Python objects inside the program, then serialize when saving or sending.
json.loads() reads JSON text and returns Python values.
import json
text = '{"name": "Maya", "skills": ["Python", "SQL"]}'
user = json.loads(text)
print(user["name"])
print(user["skills"][0])
Maya
Python
The JSON object becomes a Python dictionary with a list inside it.
json.dumps() turns Python values into JSON text.
import json
settings = {"theme": "dark", "font_size": 16}
text = json.dumps(settings, indent=2)
print(text)
{
"theme": "dark",
"font_size": 16
}
indent=2 makes the JSON easier for humans to read.
json.load() reads from a file object. json.dump() writes to a file object.
Invalid JSON raises json.JSONDecodeError.
0 of 2 checked
Try this next
0 of 3 completed
No. JSON is text. A Python dictionary is an in-memory object. json.loads() converts JSON text into Python data.
The JSON format requires double quotes around object keys and string values. Single quotes are not valid JSON.
No. JSON stores simple data such as objects, arrays, strings, numbers, booleans, and null.
Explore 500+ free tutorials across 20+ languages and frameworks.