Use this when Python reads or writes JSON files and API payloads.
JSON · Python Automation
Python handles JSON with the json module: json.load and json.loads to parse, json.dump and json.dumps with indent to write, mapping objects to dicts and arrays to lists.
1. json module parses and writes JSON.
2. Validate structure with jsonschema/pydantic.
3. Handle decode errors.
4. ensure_ascii=False for Unicode.
5. Stable ordering (sort_keys) for diffs.
JSON text → json.loads → objects → validation
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Example Narrow the fault
import json
data = json.loads(text) # str -> dict
text = json.dumps(data, indent=2) # dict -> str
with open("out.json", "w") as f:
json.dump(data, f)
Step 2 Common mistakes Narrow the fault
- Confusing
load(file) andloads(string). - Assuming keys always exist.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- json.load/loads parse; json.dump/dumps write.
- Objects become dicts, arrays become lists.
- indent makes output readable.
python3 -m json.tool file.json— validate/pretty printpython3 -c 'import json;json.load(open("f.json"))'— parse- Schema validation (jsonschema) — structure
ReferenceWhy it matters1 section · depth on demand
What it is
The json module converts between JSON text and Python dicts/lists.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Python's json module converts between JSON text and Python objects: objects become dicts, arrays become lists, null becomes None. Most automation reads API responses (often via requests' .json()) and writes structured results or reports.
import json, pathlib
data = json.loads(pathlib.Path("devices.json").read_text(encoding="utf-8"))
active = [d for d in data["results"] if d["status"]["value"] == "active"]
pathlib.Path("active.json").write_text(json.dumps(active, indent=2, sort_keys=True))
Non-JSON types need conversion: datetime objects (default=str or .isoformat()), sets (convert to sorted lists) and Decimal. Use sort_keys=True for files kept in Git, so diffs show real changes only.
Mechanics
The linked authority for bounded mechanics is RFC 8259 — The JavaScript Object Notation (JSON) Data Interchange Format; documentation support is separate from observed lab/production Evidence.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Python Automation > JSON. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.rfc-editor.org/rfc/rfc8259 · confidence 94%