Use this when parsing JSON safely in jq, PowerShell or Python with error handling.
Parsing · JSON
Parse JSON with built-in tools — jq on the command line, ConvertFrom-Json in PowerShell, json.loads in Python — and handle errors when the input is not valid JSON.
1. Parse with real JSON parsers.
2. Handle errors (invalid JSON).
3. Watch encoding (BOM).
4. Stream large files.
5. Never eval JSON.
text → JSON parser → objects or error
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 Verification path Verify boundary
The negative fixture should deliberately exercise risks such as encoding/BOM issues, delimiter/quoting ambiguity, duplicate keys, namespace drift, scalar coercion, invalid schema assumptions and parser success without consumer-valid semantics, choosing the cases most relevant to decoding/lexing/structural parse, schema validation, error location and preventing parser success from being treated as business-semantic validity.
Step 2 Common mistakes Narrow the fault
- Parsing HTML error pages as JSON.
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.
- Use jq, ConvertFrom-Json or json.loads.
- Catch parse errors on invalid input.
- PowerShell ConvertTo-Json needs -Depth for nested data.
jq -e . file.json >/dev/null && echo valid— parse checkpython3 -c 'import json;json.load(open("f.json"))'— Python parseConvertFrom-Json (PowerShell)— parse
ReferenceWhy it matters1 section · depth on demand
What it is
Parsing turns JSON text into native structures; handle errors and missing keys.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Parsing JSON turns text into native data structures so a script can work with the values. Every major language has a built-in parser; on the command line jq is the standard tool.
import json
data = json.loads(text) # str → dict/list
with open("devices.json") as f: devices = json.load(f)
json.dumps(data, indent=2, sort_keys=True)
$data = Get-Content devices.json -Raw | ConvertFrom-Json
$data | ConvertTo-Json -Depth 10 # default depth is 2 — set it explicitly
jq -r '.results[] | [.name, .site.name] | @tsv' devices.json
Handle parse errors explicitly (json.JSONDecodeError) — an HTML error page from an API is a common reason JSON parsing fails.
Example
try:
data = json.loads(text)
except json.JSONDecodeError as e:
raise SystemExit(f"bad JSON at line {e.lineno}")
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 JSON > Parsing. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://www.rfc-editor.org/rfc/rfc8259 · confidence 94%Deep reference
Healthy vs suspicious evidence
Output interpretation
For decoding/lexing/structural parse, schema validation, error location and preventing parser success from being treated as business-semantic validity, prefer typed fields and stable IDs over human presentation text.