Module lessons (1/4)
JSON: serialize and parse
json.dumps / json.loads, Python ↔ JSON type mapping, indent, sort_keys, and default.
What you'll learn in this lesson
- Serialize Python objects to JSON strings with
json.dumps()(includingindentandsort_keys). - Parse JSON payloads into Python structures with
json.loads()and learn about type mapping. - Use
json.dump()andjson.load()directly on files within awithcontext manager. - Handle non-natively serializable types (
datetime,set, custom objects) via thedefaultcallback.
Overview
JSON (JavaScript Object Notation) is the standard data exchange format between web APIs, configurations and microservices. The json module of the standard library bidirectionally converts Python structures to UTF-8 strings or JSON files.
Always remember the mnemonic rule of the four main functions: variants ending with s (dumps, loads) work on strings in memory; those without s (dump, load) work directly on streams / files opened with with.
Sections covered in this lesson
- 11. Python to JSON string with
json.dumps - 22. Parsing from JSON string with
json.loads - 33. Reading and writing to files with
dumpandload - 44. Serialize custom types with
default - 5Check for understanding
- 6Test yourself
Unlock "JSON: serialize and parse" and the full Python Course curriculum
json.dumps / json.loads, Python ↔ JSON type mapping, indent, sort_keys, and default.
Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.
- Instant access to all advanced modules in Python Course plus 17 more courses
- Hands-on browser exercises with automated test suites and solutions
- 14-day money-back guarantee processed via Paddle.com
14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.