Learn json

Encode and decode JSON, the lingua franca of APIs and config files

13 lessons · 2 hours · 4 levels · covers json Python 3.13

The json module converts between Python objects and JSON text: dicts, lists, strings, numbers, booleans and None map onto JSON's types. It is how Python programs talk to web APIs, read configuration and store simple data. Beyond the basics it offers hooks to serialise custom types, decode into your own classes and control precision and formatting.

Before you start

json builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics4 lessons · 29 min in total
2. PracticalUse it productively at work, with the usual idioms7 lessons · 57 min in total
3. AdvancedThe less common features, performance and trade-offs10 lessons · 1 hour in total
4. ExpertInternals and the edge cases experts know13 lessons · 2 hours in total

The json tree

jsondumps and loadsdumps and loadsReading and writing JSON filesReading and writingJSON filesHow Python types map to JSONHow Python typesmap to JSONPretty printing and compact outputPretty printing andcompact outputHandling invalid JSONHandling invalidJSONSerialising custom types with defaultSerialising customtypes with defaultDecoding into your own objectsDecoding into yourown objectsReading and writing filesReading and writingfilesDictionariesDictionariesExceptions in depthExceptions in depthDefining functionsDefining functionsClasses and instancesClasses andinstancesIterators and generatorsIterators andgeneratorsNumbers and arithmeticNumbers andarithmetic
Essentials and Practical shown (7 of 13 lessons); the full list is below. Click a lesson to read it.

All 13 lessons, in order

Basics

  1. dumps and loads · Essentials · 7 min

    Turn Python objects into JSON strings with json.dumps and parse JSON strings back with json.loads.

  2. Reading and writing JSON files · Essentials · 7 min

    Save and load JSON files with json.dump and json.load, using an explicit UTF-8 encoding.

  3. How Python types map to JSON · Essentials · 8 min

    Know exactly which Python types JSON supports, what they become, and what doesn't survive a round trip.

Formatting and output

  1. Pretty printing and compact output · Essentials · 7 min

    Control the look of JSON output with indent, sort_keys, separators and ensure_ascii, and pretty-print from the shell.

Robust and large data

  1. Handling invalid JSON · Practical · 8 min

    Catch json.JSONDecodeError, report where parsing failed, and validate the shape of decoded data.

  2. Large data and JSON Lines · Advanced · 10 min

    Process big datasets one record at a time with JSON Lines, and decode concatenated values with raw_decode.

Custom types

  1. Serialising custom types with default · Practical · 10 min

    Use the default= hook to encode datetimes, Decimals, sets, dataclasses and other unsupported objects.

  2. Decoding into your own objects · Practical · 10 min

    Customise decoding with object_hook, object_pairs_hook and parse_float to build objects, detect duplicates or keep exact decimals.

  3. JSONEncoder subclasses and iterencode · Advanced · 10 min

    Package custom serialisation in a JSONEncoder subclass and stream large output with iterencode.

Precision, safety and speed

  1. Numbers, precision and NaN · Advanced · 10 min

    Understand how JSON numbers map to int, float and Decimal, why money needs care, and how NaN and Infinity are handled.

  2. Parsing untrusted JSON safely · Expert · 11 min

    Defend services against hostile JSON: size and depth limits, huge integers, duplicate keys and type confusion.

  3. Canonical JSON for hashing and signing · Expert · 10 min

    Produce byte-identical JSON for equal data so it can be hashed, cached, deduplicated or signed.

  4. JSON performance and alternatives · Expert · 9 min

    Know what makes the stdlib json fast or slow, and when to switch to orjson or msgspec.