Working with JSON in Python
Working with JSON in Python
Summary
The json module converts between JSON text and Python objects. json.loads and json.dumps work on strings, and json.load and json.dump on files. Objects become dicts and arrays become lists. Tuples come back as lists, and dict keys always become strings. Use indent= for readable output and default= for types JSON can't represent.
Draft. License: CC BY 4.0 (this summary). Sources: [S1] Python Software Foundation, "json: JSON encoder and decoder (Python 3 docs)" (PSF License 2.0 docs; restated in our own words, no copied text).
Example
import json
from pathlib import Path
data = {"id": 7, "tags": ("a", "b")}
Path("out.json").write_text(json.dumps(data, indent=2))
back = json.loads(Path("out.json").read_text()) # tags is now a list
Facts
- id: F1 claim: json.dumps()/json.loads() convert to and from strings; json.dump()/json.load() write to and read from file objects. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F2 claim: Decoding maps JSON objects to dict, arrays to list, strings to str, numbers to int or float, true/false to True/False and null to None. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F3 claim: Encoding turns tuples into JSON arrays, so a round trip gives lists back. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F4 claim: JSON object keys are always strings, so non-string dict keys such as ints are converted to strings when encoded. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F5 claim: indent=2 pretty-prints; sort_keys=True sorts keys for stable output. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F6 claim: Objects JSON cannot represent (e.g. datetime, set) raise TypeError unless you pass default=, a function returning something serializable. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F7 claim: ensure_ascii is True by default and escapes non-ASCII characters; pass ensure_ascii=False to keep them as-is. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F8 claim: Invalid input raises json.JSONDecodeError, a subclass of ValueError. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
- id: F9 claim: By default Python accepts and emits NaN and Infinity, which are not valid strict JSON; allow_nan=False makes encoding them an error. confidence: medium date: 2026-11-02 sources: [S1] license: CC BY 4.0 checked_on: 2026-11-02
Caveats
- Do not use eval() on JSON text; use json.loads.
- Large integers are fine in Python but may lose precision in JavaScript consumers.
Pointers
- id: P1 answers: Full details and updates where: https://docs.python.org/3/library/json.html access: free, public web page
Source: https://docs.python.org/3/library/json.html · License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted), condensed and paraphrased; sources: Python Software Foundation, Python 3 documentation, json: JSON encoder and decoder (PSF License 2.0); restated in our own words
Cite this
iinventedlight Library. 2026. "Working with JSON in Python." iinventedlight Library, iil:note:2hnem5em, version of 2026-11-02. https://iinventedlight.com/library/reference/python-docs/python-json/. License: CC-BY-4.0. Summary of https://docs.python.org/3/library/json.html.
@misc{iil_note_2hnem5em,
author = {{iinventedlight Library}},
title = {{Working with JSON in Python}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-02},
url = {https://iinventedlight.com/library/reference/python-docs/python-json/},
note = {Permanent ID iil:note:2hnem5em. License: CC-BY-4.0. Summary of https://docs.python.org/3/library/json.html.},
}
For language models and agents: start at /llms.txt. This page in Markdown: /library/reference/python-docs/python-json.md.