Python JSON
Parse, serialize, and pretty-print JSON with correct encoding.
- python json
- json dumps
- json loads
- json.tool
- ensure_ascii
Python JSON
Parse, serialize, and pretty-print JSON with correct encoding.
Commands
| Command | What it does |
|---|---|
json.loads(text) | Parse a JSON string |
json.load(open("a.json", encoding="utf-8")) | Parse a file |
json.dumps(obj, ensure_ascii=False, indent=2) | Pretty, unicode-preserving output |
json.load with object_hook | Custom decoding |
python -m json.tool a.json | CLI pretty-printer |
Example
import json
raw = open("a.json", encoding="utf-8").read()
data = json.loads(raw)
print(json.dumps(data, ensure_ascii=False, indent=2)[:200])
FAQ
How do I serialize datetimes/decimals?
Pass default=str, or a custom JSONEncoder; JSON has no native date type.
Why ensure_ascii=False?
Keeps CJK readable in output instead of \uXXXX escapes.
Întrebări frecvente
How do I serialize datetimes/decimals?
Pass default=str, or a custom JSONEncoder; JSON has no native date type.
Why ensure_ascii=False?
Keeps CJK readable in output instead of \uXXXX escapes.
Python
Python Venv and Environments
Isolate dependencies per project with venv and pinned requirements.
Python Pip Essentials
Install, upgrade, and audit packages with pip like a pro.
Python Basics
Syntax survival kit: variables, loops, f-strings, and errors.
Python Strings
Slice, split, join, and format text efficiently.