JSON

A text format for structured data, easily converted to and from Python objects.

JSON (JavaScript Object Notation) is the common language of web APIs and config files, and it looks almost exactly like Python dictionaries and lists. Python’s built-in json module converts between JSON text and Python objects: json.loads reads text into objects, json.dumps writes objects back to text.

Example
import json

text = '{"name": "Ada", "langs": ["Python", "C"]}'
data = json.loads(text)           # JSON text -> Python dict
print(data["name"])
print(json.dumps(data["langs"]))  # Python -> JSON text
Output
Ada
["Python", "C"]

Where this shows up in real Python

JSON is how you read API responses, save structured settings, and move data between programs.

Commonly used JSON tools

  • json.loads(text) — parse JSON text into Python
  • json.dumps(obj) — turn Python into JSON text
  • json.load(file) — read JSON from a file
  • json.dump(obj, file) — write JSON to a file
  • indent=2 — pretty-print with indentation

Official documentation: Python Library Reference: json

Related terms