output
json.dumps returns a JSON-formatted str. json.dump writes to a file-like object that accepts str, not bytes.
"str"ensure_ascii
ensure_ascii defaults to true and escapes non-ASCII characters. Set it to false to output those characters as-is.
trueallow_nan
allow_nan defaults to true. Set it to false to raise ValueError when serializing NaN or infinities instead of emitting their JavaScript equivalents.
truesort_keys
sort_keys defaults to false. When true, dictionary output is sorted by key.
falseTry the example
import json
text = json.dumps({"name": "한글", "count": 2},
ensure_ascii=False, allow_nan=False, sort_keys=True)
print(text)Use allow_nan=False when the receiving system requires JSON numbers rather than JavaScript non-finite values. This is an editorial recommendation based on the documented option.
Source and reuse
- Original
- Python 3.13.0: json encoder and decoder
- Version / observed
- CPython v3.13.0 / 2026-11-24T04:53:54.630820+00:00
- Use conditions
- PSF License Version 2
Copyright (c) 2001-2024 Python Software Foundation; All Rights Reserved
Full license text - SHA256
- 6533f6d5be67f903194ba4b92335d079552edb69c4c8fd45d6215de60358dd8e
The hash identifies the archived source bytes retrieved from the version tag, not a publisher-issued signature. These are condensed notes with newly written examples, not a verbatim replacement for the manual.
Archived source textInspect the complete public JSON
{
"schema": "llmsdocs-editorial-v1",
"id": "encode",
"language": "en",
"revision": 1,
"edition": "2026-11-24",
"kind": "editorial",
"title": "Write JSON without losing intent",
"intro": "Choose a string or file output, then decide how to handle Unicode and non-finite numbers. These notes describe Python 3.13.0, not every JSON implementation.",
"html_url": "https://llmsdocs.com/en/manual/encode",
"json_url": "https://llmsdocs.com/en/manual/encode.json",
"source": {
"id": "python-json-313",
"title": "Python 3.13.0: json encoder and decoder",
"url": "https://github.com/python/cpython/blob/v3.13.0/Doc/library/json.rst",
"version": "CPython v3.13.0",
"observed_at": "2026-11-24T04:53:54.630820+00:00",
"sha256": "6533f6d5be67f903194ba4b92335d079552edb69c4c8fd45d6215de60358dd8e",
"license": "PSF License Version 2",
"license_url": "https://github.com/python/cpython/blob/v3.13.0/LICENSE",
"copyright": "Copyright (c) 2001-2024 Python Software Foundation; All Rights Reserved",
"snapshot": "/manual-source/json.rst",
"license_snapshot": "/manual-source/LICENSE.txt"
},
"changes": "The source was condensed into topic notes. Examples are newly written; Korean notes are translations.",
"disclosure": "AI-assisted editorial notes, not official Python documentation or customer submissions.",
"facts": [
{
"id": "encode-1",
"key": "output",
"value": "str",
"text": "json.dumps returns a JSON-formatted str. json.dump writes to a file-like object that accepts str, not bytes.",
"source_id": "python-json-313",
"source_lines": "148-163, 218-225"
},
{
"id": "encode-2",
"key": "ensure_ascii",
"value": true,
"text": "ensure_ascii defaults to true and escapes non-ASCII characters. Set it to false to output those characters as-is.",
"source_id": "python-json-313",
"source_lines": "165-167"
},
{
"id": "encode-3",
"key": "allow_nan",
"value": true,
"text": "allow_nan defaults to true. Set it to false to raise ValueError when serializing NaN or infinities instead of emitting their JavaScript equivalents.",
"source_id": "python-json-313",
"source_lines": "173-177"
},
{
"id": "encode-4",
"key": "sort_keys",
"value": false,
"text": "sort_keys defaults to false. When true, dictionary output is sorted by key.",
"source_id": "python-json-313",
"source_lines": "202-203"
}
],
"example": "import json\ntext = json.dumps({\"name\": \"한글\", \"count\": 2},\n ensure_ascii=False, allow_nan=False, sort_keys=True)\nprint(text)",
"editorial_note": "Use allow_nan=False when the receiving system requires JSON numbers rather than JavaScript non-finite values. This is an editorial recommendation based on the documented option."
}The source was condensed into topic notes. Examples are newly written; Korean notes are translations.