Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

JSON · Python Automation

Direct answer

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.

When to use this

Use this when Python reads or writes JSON files and API payloads.

First 30 seconds

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.

Operational verification

JSON text → json.loads → objects → validation

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
JSON parsing and validation
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformJSON parsing and validation · Automation implementations are tool and platform specific. Use the adjacent Domain for the technology being automated and this Domain for automation behavior.
Scope / prerequisiteUse this when Python reads or writes JSON files and API payloads.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

2 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

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) and loads (string).
  • Assuming keys always exist.
Evidence references

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.

observation · 1
Key points
  • json.load/loads parse; json.dump/dumps write.
  • Objects become dicts, arrays become lists.
  • indent makes output readable.
Key checks
  • python3 -m json.tool file.json — validate/pretty print
  • python3 -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.

Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docJSON parsing and validation current

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%
Confirmation

Confirm action

Continue?