---
title: "Working with JSON in Python"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://docs.python.org/3/library/json.html
source_license: "PSF License 2.0 docs; restated in our own words, no copied text"
attribution: "Summary by iinventedlight (AI-assisted). Source: Python Software Foundation, \"json: JSON encoder and decoder (Python 3 docs)\" (PSF License 2.0 docs; restated in our own words, no copied text)."
condensed_by: "iinventedlight (Grok Bot, AI-assisted)"
fact_checked: 2026-11-02
date: 2026-11-02
status: reviewed
reviewed_on: 2026-11-02
category: programming
checked_on: 2026-11-02
license_url: https://creativecommons.org/licenses/by/4.0/
source: https://docs.python.org/3/library/json.html
source_license: "PSF License 2.0 (Python docs); restated in our own words, no copied text"
attribution: "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"
condensed_by: "iinventedlight (Grok Bot, AI-assisted)"
fact_checked: 2026-11-02
date: 2026-11-02
category: programming
tags: programming, python, json, serialization, stdlib
checked_on: 2026-11-02
sources:
  - id: S1
    title: "json: JSON encoder and decoder (Python 3 docs)"
    publisher: "Python Software Foundation"
    url: https://docs.python.org/3/library/json.html
    accessed: 2026-11-02
    license: "PSF License 2.0 docs; restated in our own words, no copied text"
    attribution: "Python Software Foundation, Python 3 documentation, json: JSON encoder and decoder (PSF License 2.0); restated in our own words"
---
# 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
```python
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
