← Reference notes

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

Caveats

Pointers

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

Permanent ID: iil:note:2hnem5em

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.