Working with JSON Files in Python
JSON (JavaScript Object Notation) is a lightweight, text-based format for structured data, and is the de facto standard for APIs and configuration files across virtually every modern programming language. Python's built-in json module converts directly between JSON text and Python's own dictionaries and lists, in both directions.
import json
Brings in the module, part of the standard library.
json.dumps(python_object)
Converts a Python dictionary or list into a JSON-formatted string.
json.loads(json_string)
Converts a JSON string back into the equivalent Python dictionary or list.
json.dump() and json.load()
Write JSON directly to an open file, or read JSON directly from one, without a separate string step.
JSON's types map closely to Python's
Objects become dictionaries, arrays become lists, and true/false/null become True/False/None.
import json data = {"name": "Ada", "age": 28, "active": True} json_string = json.dumps(data)print(json_string) with open("person.json", "w") as f: json.dump(data, f) with open("person.json", "r") as f: loaded = json.load(f) print(loaded["name"])Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Python yourself — building beats watching.