Learn Python
>>> file_handling.json_files

Working with JSON Files in Python

#concept

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.

#how it works
1

import json

Brings in the module, part of the standard library.

2

json.dumps(python_object)

Converts a Python dictionary or list into a JSON-formatted string.

3

json.loads(json_string)

Converts a JSON string back into the equivalent Python dictionary or list.

4

json.dump() and json.load()

Write JSON directly to an open file, or read JSON directly from one, without a separate string step.

5

JSON's types map closely to Python's

Objects become dictionaries, arrays become lists, and true/false/null become True/False/None.

#example
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"])
Output
{"name": "Ada", "age": 28, "active": true}
Ada
1json.dumps(data) converts the Python dictionary into a JSON string — notice True becomes lowercase true, matching JSON’s own syntax rather than Python’s.
2json.dump(data, f) writes that same JSON representation directly into an open file, without needing a separate .write() call.
3json.load(f) reads the file back and parses it straight into a Python dictionary again, complete with the correct types restored.
4loaded["name"] accesses it exactly like any other dictionary, confirming the round trip preserved the data correctly.
#use cases
Reading and writing configuration files in a format virtually every tool and language can also read
Sending and receiving data from web APIs, which almost universally communicate using JSON
Saving a program's structured data (settings, saved state) to disk between runs
Converting between JSON text (for storage or network transfer) and Python objects (for actual use in code)

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Python yourself — building beats watching.