Skip to main content
Module 8 · Lesson 1 of 429/36 in the course~14 min
Module lessons (1/4)

JSON: serialize and parse

Lesson preview5 hands-on exercises readyPython Course · Module 8 — Essential standard library

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() (including indent and sort_keys).
  • Parse JSON payloads into Python structures with json.loads() and learn about type mapping.
  • Use json.dump() and json.load() directly on files within a with context manager.
  • Handle non-natively serializable types (datetime, set, custom objects) via the default callback.

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

  1. 11. Python to JSON string with json.dumps
  2. 22. Parsing from JSON string with json.loads
  3. 33. Reading and writing to files with dump and load
  4. 44. Serialize custom types with default
  5. 5Check for understanding
  6. 6Test yourself
Pro Lesson · Module 8 — Essential standard library

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.