gooderp18绿色标准版
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1099 行
101KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="json — JSON encoder and decoder" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/json.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/json/__init__.py JSON (JavaScript Object Notation), specified by RFC 7159(which obsoletes RFC 4627) and by ECMA-404, is a lightweight data interchange format inspired by JavaScript..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/json/__init__.py JSON (JavaScript Object Notation), specified by RFC 7159(which obsoletes RFC 4627) and by ECMA-404, is a lightweight data interchange format inspired by JavaScript..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>json — JSON encoder and decoder &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="mailbox — Manipulate mailboxes in various formats" href="mailbox.html" />
  33. <link rel="prev" title="email.iterators: Iterators" href="email.iterators.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/json.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code> — JSON encoder and decoder</a><ul>
  86. <li><a class="reference internal" href="#basic-usage">Basic Usage</a></li>
  87. <li><a class="reference internal" href="#encoders-and-decoders">Encoders and Decoders</a></li>
  88. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  89. <li><a class="reference internal" href="#standard-compliance-and-interoperability">Standard Compliance and Interoperability</a><ul>
  90. <li><a class="reference internal" href="#character-encodings">Character Encodings</a></li>
  91. <li><a class="reference internal" href="#infinite-and-nan-number-values">Infinite and NaN Number Values</a></li>
  92. <li><a class="reference internal" href="#repeated-names-within-an-object">Repeated Names Within an Object</a></li>
  93. <li><a class="reference internal" href="#top-level-non-object-non-array-values">Top-level Non-Object, Non-Array Values</a></li>
  94. <li><a class="reference internal" href="#implementation-limitations">Implementation Limitations</a></li>
  95. </ul>
  96. </li>
  97. <li><a class="reference internal" href="#module-json.tool">Command Line Interface</a><ul>
  98. <li><a class="reference internal" href="#command-line-options">Command line options</a></li>
  99. </ul>
  100. </li>
  101. </ul>
  102. </li>
  103. </ul>
  104. </div>
  105. <div>
  106. <h4>Previous topic</h4>
  107. <p class="topless"><a href="email.iterators.html"
  108. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.iterators</span></code>: Iterators</a></p>
  109. </div>
  110. <div>
  111. <h4>Next topic</h4>
  112. <p class="topless"><a href="mailbox.html"
  113. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mailbox</span></code> — Manipulate mailboxes in various formats</a></p>
  114. </div>
  115. <div role="note" aria-label="source link">
  116. <h3>This Page</h3>
  117. <ul class="this-page-menu">
  118. <li><a href="../bugs.html">Report a Bug</a></li>
  119. <li>
  120. <a href="https://github.com/python/cpython/blob/main/Doc/library/json.rst"
  121. rel="nofollow">Show Source
  122. </a>
  123. </li>
  124. </ul>
  125. </div>
  126. </nav>
  127. </div>
  128. </div>
  129. <div class="related" role="navigation" aria-label="related navigation">
  130. <h3>Navigation</h3>
  131. <ul>
  132. <li class="right" style="margin-right: 10px">
  133. <a href="../genindex.html" title="General Index"
  134. accesskey="I">index</a></li>
  135. <li class="right" >
  136. <a href="../py-modindex.html" title="Python Module Index"
  137. >modules</a> |</li>
  138. <li class="right" >
  139. <a href="mailbox.html" title="mailbox — Manipulate mailboxes in various formats"
  140. accesskey="N">next</a> |</li>
  141. <li class="right" >
  142. <a href="email.iterators.html" title="email.iterators: Iterators"
  143. accesskey="P">previous</a> |</li>
  144. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  145. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  146. <li class="switchers">
  147. <div class="language_switcher_placeholder"></div>
  148. <div class="version_switcher_placeholder"></div>
  149. </li>
  150. <li>
  151. </li>
  152. <li id="cpython-language-and-version">
  153. <a href="../index.html">3.12.3 Documentation</a> &#187;
  154. </li>
  155. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  156. <li class="nav-item nav-item-2"><a href="netdata.html" accesskey="U">Internet Data Handling</a> &#187;</li>
  157. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code> — JSON encoder and decoder</a></li>
  158. <li class="right">
  159. <div class="inline-search" role="search">
  160. <form class="inline-search" action="../search.html" method="get">
  161. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  162. <input type="submit" value="Go" />
  163. </form>
  164. </div>
  165. |
  166. </li>
  167. <li class="right">
  168. <label class="theme-selector-label">
  169. Theme
  170. <select class="theme-selector" oninput="activateTheme(this.value)">
  171. <option value="auto" selected>Auto</option>
  172. <option value="light">Light</option>
  173. <option value="dark">Dark</option>
  174. </select>
  175. </label> |</li>
  176. </ul>
  177. </div>
  178. <div class="document">
  179. <div class="documentwrapper">
  180. <div class="bodywrapper">
  181. <div class="body" role="main">
  182. <section id="module-json">
  183. <span id="json-json-encoder-and-decoder"></span><h1><a class="reference internal" href="#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code></a> — JSON encoder and decoder<a class="headerlink" href="#module-json" title="Link to this heading">¶</a></h1>
  184. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/json/__init__.py">Lib/json/__init__.py</a></p>
  185. <hr class="docutils" />
  186. <p><a class="reference external" href="https://json.org">JSON (JavaScript Object Notation)</a>, specified by
  187. <span class="target" id="index-0"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc7159.html"><strong>RFC 7159</strong></a> (which obsoletes <span class="target" id="index-1"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4627.html"><strong>RFC 4627</strong></a>) and by
  188. <a class="reference external" href="https://www.ecma-international.org/publications-and-standards/standards/ecma-404/">ECMA-404</a>,
  189. is a lightweight data interchange format inspired by
  190. <a class="reference external" href="https://en.wikipedia.org/wiki/JavaScript">JavaScript</a> object literal syntax
  191. (although it is not a strict subset of JavaScript <a class="footnote-reference brackets" href="#rfc-errata" id="id1" role="doc-noteref"><span class="fn-bracket">[</span>1<span class="fn-bracket">]</span></a> ).</p>
  192. <div class="admonition warning">
  193. <p class="admonition-title">Warning</p>
  194. <p>Be cautious when parsing JSON data from untrusted sources. A malicious
  195. JSON string may cause the decoder to consume considerable CPU and memory
  196. resources. Limiting the size of data to be parsed is recommended.</p>
  197. </div>
  198. <p><a class="reference internal" href="#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code></a> exposes an API familiar to users of the standard library
  199. <a class="reference internal" href="marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">marshal</span></code></a> and <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> modules.</p>
  200. <p>Encoding basic Python object hierarchies:</p>
  201. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">json</span>
  202. <span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">([</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;bar&#39;</span><span class="p">:</span> <span class="p">(</span><span class="s1">&#39;baz&#39;</span><span class="p">,</span> <span class="kc">None</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mi">2</span><span class="p">)}])</span>
  203. <span class="go">&#39;[&quot;foo&quot;, {&quot;bar&quot;: [&quot;baz&quot;, null, 1.0, 2]}]&#39;</span>
  204. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="s2">&quot;</span><span class="se">\&quot;</span><span class="s2">foo</span><span class="se">\b</span><span class="s2">ar&quot;</span><span class="p">))</span>
  205. <span class="go">&quot;\&quot;foo\bar&quot;</span>
  206. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\u1234</span><span class="s1">&#39;</span><span class="p">))</span>
  207. <span class="go">&quot;\u1234&quot;</span>
  208. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">&#39;</span><span class="p">))</span>
  209. <span class="go">&quot;\\&quot;</span>
  210. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">({</span><span class="s2">&quot;c&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;b&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span> <span class="s2">&quot;a&quot;</span><span class="p">:</span> <span class="mi">0</span><span class="p">},</span> <span class="n">sort_keys</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
  211. <span class="go">{&quot;a&quot;: 0, &quot;b&quot;: 0, &quot;c&quot;: 0}</span>
  212. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">io</span> <span class="kn">import</span> <span class="n">StringIO</span>
  213. <span class="gp">&gt;&gt;&gt; </span><span class="n">io</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">()</span>
  214. <span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">dump</span><span class="p">([</span><span class="s1">&#39;streaming API&#39;</span><span class="p">],</span> <span class="n">io</span><span class="p">)</span>
  215. <span class="gp">&gt;&gt;&gt; </span><span class="n">io</span><span class="o">.</span><span class="n">getvalue</span><span class="p">()</span>
  216. <span class="go">&#39;[&quot;streaming API&quot;]&#39;</span>
  217. </pre></div>
  218. </div>
  219. <p>Compact encoding:</p>
  220. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">json</span>
  221. <span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="p">{</span><span class="s1">&#39;4&#39;</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span> <span class="s1">&#39;6&#39;</span><span class="p">:</span> <span class="mi">7</span><span class="p">}],</span> <span class="n">separators</span><span class="o">=</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">,</span> <span class="s1">&#39;:&#39;</span><span class="p">))</span>
  222. <span class="go">&#39;[1,2,3,{&quot;4&quot;:5,&quot;6&quot;:7}]&#39;</span>
  223. </pre></div>
  224. </div>
  225. <p>Pretty printing:</p>
  226. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">json</span>
  227. <span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">({</span><span class="s1">&#39;4&#39;</span><span class="p">:</span> <span class="mi">5</span><span class="p">,</span> <span class="s1">&#39;6&#39;</span><span class="p">:</span> <span class="mi">7</span><span class="p">},</span> <span class="n">sort_keys</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
  228. <span class="go">{</span>
  229. <span class="go"> &quot;4&quot;: 5,</span>
  230. <span class="go"> &quot;6&quot;: 7</span>
  231. <span class="go">}</span>
  232. </pre></div>
  233. </div>
  234. <p>Decoding JSON:</p>
  235. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">json</span>
  236. <span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="s1">&#39;[&quot;foo&quot;, {&quot;bar&quot;:[&quot;baz&quot;, null, 1.0, 2]}]&#39;</span><span class="p">)</span>
  237. <span class="go">[&#39;foo&#39;, {&#39;bar&#39;: [&#39;baz&#39;, None, 1.0, 2]}]</span>
  238. <span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="s1">&#39;&quot;</span><span class="se">\\</span><span class="s1">&quot;foo</span><span class="se">\\</span><span class="s1">bar&quot;&#39;</span><span class="p">)</span>
  239. <span class="go">&#39;&quot;foo\x08ar&#39;</span>
  240. <span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">io</span> <span class="kn">import</span> <span class="n">StringIO</span>
  241. <span class="gp">&gt;&gt;&gt; </span><span class="n">io</span> <span class="o">=</span> <span class="n">StringIO</span><span class="p">(</span><span class="s1">&#39;[&quot;streaming API&quot;]&#39;</span><span class="p">)</span>
  242. <span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">io</span><span class="p">)</span>
  243. <span class="go">[&#39;streaming API&#39;]</span>
  244. </pre></div>
  245. </div>
  246. <p>Specializing JSON object decoding:</p>
  247. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">json</span>
  248. <span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">as_complex</span><span class="p">(</span><span class="n">dct</span><span class="p">):</span>
  249. <span class="gp">... </span> <span class="k">if</span> <span class="s1">&#39;__complex__&#39;</span> <span class="ow">in</span> <span class="n">dct</span><span class="p">:</span>
  250. <span class="gp">... </span> <span class="k">return</span> <span class="nb">complex</span><span class="p">(</span><span class="n">dct</span><span class="p">[</span><span class="s1">&#39;real&#39;</span><span class="p">],</span> <span class="n">dct</span><span class="p">[</span><span class="s1">&#39;imag&#39;</span><span class="p">])</span>
  251. <span class="gp">... </span> <span class="k">return</span> <span class="n">dct</span>
  252. <span class="gp">...</span>
  253. <span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="s1">&#39;{&quot;__complex__&quot;: true, &quot;real&quot;: 1, &quot;imag&quot;: 2}&#39;</span><span class="p">,</span>
  254. <span class="gp">... </span> <span class="n">object_hook</span><span class="o">=</span><span class="n">as_complex</span><span class="p">)</span>
  255. <span class="go">(1+2j)</span>
  256. <span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">decimal</span>
  257. <span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="s1">&#39;1.1&#39;</span><span class="p">,</span> <span class="n">parse_float</span><span class="o">=</span><span class="n">decimal</span><span class="o">.</span><span class="n">Decimal</span><span class="p">)</span>
  258. <span class="go">Decimal(&#39;1.1&#39;)</span>
  259. </pre></div>
  260. </div>
  261. <p>Extending <a class="reference internal" href="#json.JSONEncoder" title="json.JSONEncoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">JSONEncoder</span></code></a>:</p>
  262. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">json</span>
  263. <span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">ComplexEncoder</span><span class="p">(</span><span class="n">json</span><span class="o">.</span><span class="n">JSONEncoder</span><span class="p">):</span>
  264. <span class="gp">... </span> <span class="k">def</span> <span class="nf">default</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">obj</span><span class="p">):</span>
  265. <span class="gp">... </span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">obj</span><span class="p">,</span> <span class="nb">complex</span><span class="p">):</span>
  266. <span class="gp">... </span> <span class="k">return</span> <span class="p">[</span><span class="n">obj</span><span class="o">.</span><span class="n">real</span><span class="p">,</span> <span class="n">obj</span><span class="o">.</span><span class="n">imag</span><span class="p">]</span>
  267. <span class="gp">... </span> <span class="c1"># Let the base class default method raise the TypeError</span>
  268. <span class="gp">... </span> <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">default</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
  269. <span class="gp">...</span>
  270. <span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="n">j</span><span class="p">,</span> <span class="bp">cls</span><span class="o">=</span><span class="n">ComplexEncoder</span><span class="p">)</span>
  271. <span class="go">&#39;[2.0, 1.0]&#39;</span>
  272. <span class="gp">&gt;&gt;&gt; </span><span class="n">ComplexEncoder</span><span class="p">()</span><span class="o">.</span><span class="n">encode</span><span class="p">(</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="n">j</span><span class="p">)</span>
  273. <span class="go">&#39;[2.0, 1.0]&#39;</span>
  274. <span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">ComplexEncoder</span><span class="p">()</span><span class="o">.</span><span class="n">iterencode</span><span class="p">(</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="n">j</span><span class="p">))</span>
  275. <span class="go">[&#39;[2.0&#39;, &#39;, 1.0&#39;, &#39;]&#39;]</span>
  276. </pre></div>
  277. </div>
  278. <p>Using <a class="reference internal" href="#module-json.tool" title="json.tool: A command line to validate and pretty-print JSON."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json.tool</span></code></a> from the shell to validate and pretty-print:</p>
  279. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span><span class="nb">echo</span><span class="w"> </span><span class="s1">&#39;{&quot;json&quot;:&quot;obj&quot;}&#39;</span><span class="w"> </span><span class="p">|</span><span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>json.tool
  280. <span class="go">{</span>
  281. <span class="go"> &quot;json&quot;: &quot;obj&quot;</span>
  282. <span class="go">}</span>
  283. <span class="gp">$ </span><span class="nb">echo</span><span class="w"> </span><span class="s1">&#39;{1.2:3.4}&#39;</span><span class="w"> </span><span class="p">|</span><span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>json.tool
  284. <span class="go">Expecting property name enclosed in double quotes: line 1 column 2 (char 1)</span>
  285. </pre></div>
  286. </div>
  287. <p>See <a class="reference internal" href="#json-commandline"><span class="std std-ref">Command Line Interface</span></a> for detailed documentation.</p>
  288. <div class="admonition note">
  289. <p class="admonition-title">Note</p>
  290. <p>JSON is a subset of <a class="reference external" href="https://yaml.org/">YAML</a> 1.2. The JSON produced by
  291. this module’s default settings (in particular, the default <em>separators</em>
  292. value) is also a subset of YAML 1.0 and 1.1. This module can thus also be
  293. used as a YAML serializer.</p>
  294. </div>
  295. <div class="admonition note">
  296. <p class="admonition-title">Note</p>
  297. <p>This module’s encoders and decoders preserve input and output order by
  298. default. Order is only lost if the underlying containers are unordered.</p>
  299. </div>
  300. <section id="basic-usage">
  301. <h2>Basic Usage<a class="headerlink" href="#basic-usage" title="Link to this heading">¶</a></h2>
  302. <dl class="py function">
  303. <dt class="sig sig-object py" id="json.dump">
  304. <span class="sig-prename descclassname"><span class="pre">json.</span></span><span class="sig-name descname"><span class="pre">dump</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">fp</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">skipkeys</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ensure_ascii</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">check_circular</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">allow_nan</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cls</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">indent</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">separators</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sort_keys</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json.dump" title="Link to this definition">¶</a></dt>
  305. <dd><p>Serialize <em>obj</em> as a JSON formatted stream to <em>fp</em> (a <code class="docutils literal notranslate"><span class="pre">.write()</span></code>-supporting
  306. <a class="reference internal" href="../glossary.html#term-file-like-object"><span class="xref std std-term">file-like object</span></a>) using this <a class="reference internal" href="#py-to-json-table"><span class="std std-ref">conversion table</span></a>.</p>
  307. <p>If <em>skipkeys</em> is true (default: <code class="docutils literal notranslate"><span class="pre">False</span></code>), then dict keys that are not
  308. of a basic type (<a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>, <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>, <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>, <a class="reference internal" href="functions.html#bool" title="bool"><code class="xref py py-class docutils literal notranslate"><span class="pre">bool</span></code></a>,
  309. <code class="docutils literal notranslate"><span class="pre">None</span></code>) will be skipped instead of raising a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.</p>
  310. <p>The <a class="reference internal" href="#module-json" title="json: Encode and decode the JSON format."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code></a> module always produces <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> objects, not
  311. <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> objects. Therefore, <code class="docutils literal notranslate"><span class="pre">fp.write()</span></code> must support <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>
  312. input.</p>
  313. <p>If <em>ensure_ascii</em> is true (the default), the output is guaranteed to
  314. have all incoming non-ASCII characters escaped. If <em>ensure_ascii</em> is
  315. false, these characters will be output as-is.</p>
  316. <p>If <em>check_circular</em> is false (default: <code class="docutils literal notranslate"><span class="pre">True</span></code>), then the circular
  317. reference check for container types will be skipped and a circular reference
  318. will result in a <a class="reference internal" href="exceptions.html#RecursionError" title="RecursionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RecursionError</span></code></a> (or worse).</p>
  319. <p>If <em>allow_nan</em> is false (default: <code class="docutils literal notranslate"><span class="pre">True</span></code>), then it will be a
  320. <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> to serialize out of range <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a> values (<code class="docutils literal notranslate"><span class="pre">nan</span></code>,
  321. <code class="docutils literal notranslate"><span class="pre">inf</span></code>, <code class="docutils literal notranslate"><span class="pre">-inf</span></code>) in strict compliance of the JSON specification.
  322. If <em>allow_nan</em> is true, their JavaScript equivalents (<code class="docutils literal notranslate"><span class="pre">NaN</span></code>,
  323. <code class="docutils literal notranslate"><span class="pre">Infinity</span></code>, <code class="docutils literal notranslate"><span class="pre">-Infinity</span></code>) will be used.</p>
  324. <p>If <em>indent</em> is a non-negative integer or string, then JSON array elements and
  325. object members will be pretty-printed with that indent level. An indent level
  326. of 0, negative, or <code class="docutils literal notranslate"><span class="pre">&quot;&quot;</span></code> will only insert newlines. <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default)
  327. selects the most compact representation. Using a positive integer indent
  328. indents that many spaces per level. If <em>indent</em> is a string (such as <code class="docutils literal notranslate"><span class="pre">&quot;\t&quot;</span></code>),
  329. that string is used to indent each level.</p>
  330. <div class="versionchanged">
  331. <p><span class="versionmodified changed">Changed in version 3.2: </span>Allow strings for <em>indent</em> in addition to integers.</p>
  332. </div>
  333. <p>If specified, <em>separators</em> should be an <code class="docutils literal notranslate"><span class="pre">(item_separator,</span> <span class="pre">key_separator)</span></code>
  334. tuple. The default is <code class="docutils literal notranslate"><span class="pre">(',</span> <span class="pre">',</span> <span class="pre">':</span> <span class="pre">')</span></code> if <em>indent</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> and
  335. <code class="docutils literal notranslate"><span class="pre">(',',</span> <span class="pre">':</span> <span class="pre">')</span></code> otherwise. To get the most compact JSON representation,
  336. you should specify <code class="docutils literal notranslate"><span class="pre">(',',</span> <span class="pre">':')</span></code> to eliminate whitespace.</p>
  337. <div class="versionchanged">
  338. <p><span class="versionmodified changed">Changed in version 3.4: </span>Use <code class="docutils literal notranslate"><span class="pre">(',',</span> <span class="pre">':</span> <span class="pre">')</span></code> as default if <em>indent</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  339. </div>
  340. <p>If specified, <em>default</em> should be a function that gets called for objects that
  341. can’t otherwise be serialized. It should return a JSON encodable version of
  342. the object or raise a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>. If not specified, <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>
  343. is raised.</p>
  344. <p>If <em>sort_keys</em> is true (default: <code class="docutils literal notranslate"><span class="pre">False</span></code>), then the output of
  345. dictionaries will be sorted by key.</p>
  346. <p>To use a custom <a class="reference internal" href="#json.JSONEncoder" title="json.JSONEncoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">JSONEncoder</span></code></a> subclass (e.g. one that overrides the
  347. <a class="reference internal" href="#json.JSONEncoder.default" title="json.JSONEncoder.default"><code class="xref py py-meth docutils literal notranslate"><span class="pre">default()</span></code></a> method to serialize additional types), specify it with the
  348. <em>cls</em> kwarg; otherwise <a class="reference internal" href="#json.JSONEncoder" title="json.JSONEncoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">JSONEncoder</span></code></a> is used.</p>
  349. <div class="versionchanged">
  350. <p><span class="versionmodified changed">Changed in version 3.6: </span>All optional parameters are now <a class="reference internal" href="../glossary.html#keyword-only-parameter"><span class="std std-ref">keyword-only</span></a>.</p>
  351. </div>
  352. <div class="admonition note">
  353. <p class="admonition-title">Note</p>
  354. <p>Unlike <a class="reference internal" href="pickle.html#module-pickle" title="pickle: Convert Python objects to streams of bytes and back."><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> and <a class="reference internal" href="marshal.html#module-marshal" title="marshal: Convert Python objects to streams of bytes and back (with different constraints)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">marshal</span></code></a>, JSON is not a framed protocol,
  355. so trying to serialize multiple objects with repeated calls to
  356. <a class="reference internal" href="#json.dump" title="json.dump"><code class="xref py py-func docutils literal notranslate"><span class="pre">dump()</span></code></a> using the same <em>fp</em> will result in an invalid JSON file.</p>
  357. </div>
  358. </dd></dl>
  359. <dl class="py function">
  360. <dt class="sig sig-object py" id="json.dumps">
  361. <span class="sig-prename descclassname"><span class="pre">json.</span></span><span class="sig-name descname"><span class="pre">dumps</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">skipkeys</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ensure_ascii</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">check_circular</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">allow_nan</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cls</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">indent</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">separators</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sort_keys</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json.dumps" title="Link to this definition">¶</a></dt>
  362. <dd><p>Serialize <em>obj</em> to a JSON formatted <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> using this <a class="reference internal" href="#py-to-json-table"><span class="std std-ref">conversion
  363. table</span></a>. The arguments have the same meaning as in
  364. <a class="reference internal" href="#json.dump" title="json.dump"><code class="xref py py-func docutils literal notranslate"><span class="pre">dump()</span></code></a>.</p>
  365. <div class="admonition note">
  366. <p class="admonition-title">Note</p>
  367. <p>Keys in key/value pairs of JSON are always of the type <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>. When
  368. a dictionary is converted into JSON, all the keys of the dictionary are
  369. coerced to strings. As a result of this, if a dictionary is converted
  370. into JSON and then back into a dictionary, the dictionary may not equal
  371. the original one. That is, <code class="docutils literal notranslate"><span class="pre">loads(dumps(x))</span> <span class="pre">!=</span> <span class="pre">x</span></code> if x has non-string
  372. keys.</p>
  373. </div>
  374. </dd></dl>
  375. <dl class="py function">
  376. <dt class="sig sig-object py" id="json.load">
  377. <span class="sig-prename descclassname"><span class="pre">json.</span></span><span class="sig-name descname"><span class="pre">load</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">fp</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cls</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">object_hook</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parse_float</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parse_int</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parse_constant</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">object_pairs_hook</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json.load" title="Link to this definition">¶</a></dt>
  378. <dd><p>Deserialize <em>fp</em> (a <code class="docutils literal notranslate"><span class="pre">.read()</span></code>-supporting <a class="reference internal" href="../glossary.html#term-text-file"><span class="xref std std-term">text file</span></a> or
  379. <a class="reference internal" href="../glossary.html#term-binary-file"><span class="xref std std-term">binary file</span></a> containing a JSON document) to a Python object using
  380. this <a class="reference internal" href="#json-to-py-table"><span class="std std-ref">conversion table</span></a>.</p>
  381. <p><em>object_hook</em> is an optional function that will be called with the result of
  382. any object literal decoded (a <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>). The return value of
  383. <em>object_hook</em> will be used instead of the <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>. This feature can be used
  384. to implement custom decoders (e.g. <a class="reference external" href="https://www.jsonrpc.org">JSON-RPC</a>
  385. class hinting).</p>
  386. <p><em>object_pairs_hook</em> is an optional function that will be called with the
  387. result of any object literal decoded with an ordered list of pairs. The
  388. return value of <em>object_pairs_hook</em> will be used instead of the
  389. <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>. This feature can be used to implement custom decoders.
  390. If <em>object_hook</em> is also defined, the <em>object_pairs_hook</em> takes priority.</p>
  391. <div class="versionchanged">
  392. <p><span class="versionmodified changed">Changed in version 3.1: </span>Added support for <em>object_pairs_hook</em>.</p>
  393. </div>
  394. <p><em>parse_float</em>, if specified, will be called with the string of every JSON
  395. float to be decoded. By default, this is equivalent to <code class="docutils literal notranslate"><span class="pre">float(num_str)</span></code>.
  396. This can be used to use another datatype or parser for JSON floats
  397. (e.g. <a class="reference internal" href="decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">decimal.Decimal</span></code></a>).</p>
  398. <p><em>parse_int</em>, if specified, will be called with the string of every JSON int
  399. to be decoded. By default, this is equivalent to <code class="docutils literal notranslate"><span class="pre">int(num_str)</span></code>. This can
  400. be used to use another datatype or parser for JSON integers
  401. (e.g. <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>).</p>
  402. <div class="versionchanged">
  403. <p><span class="versionmodified changed">Changed in version 3.11: </span>The default <em>parse_int</em> of <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-func docutils literal notranslate"><span class="pre">int()</span></code></a> now limits the maximum length of
  404. the integer string via the interpreter’s <a class="reference internal" href="stdtypes.html#int-max-str-digits"><span class="std std-ref">integer string
  405. conversion length limitation</span></a> to help avoid denial
  406. of service attacks.</p>
  407. </div>
  408. <p><em>parse_constant</em>, if specified, will be called with one of the following
  409. strings: <code class="docutils literal notranslate"><span class="pre">'-Infinity'</span></code>, <code class="docutils literal notranslate"><span class="pre">'Infinity'</span></code>, <code class="docutils literal notranslate"><span class="pre">'NaN'</span></code>.
  410. This can be used to raise an exception if invalid JSON numbers
  411. are encountered.</p>
  412. <div class="versionchanged">
  413. <p><span class="versionmodified changed">Changed in version 3.1: </span><em>parse_constant</em> doesn’t get called on ‘null’, ‘true’, ‘false’ anymore.</p>
  414. </div>
  415. <p>To use a custom <a class="reference internal" href="#json.JSONDecoder" title="json.JSONDecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">JSONDecoder</span></code></a> subclass, specify it with the <code class="docutils literal notranslate"><span class="pre">cls</span></code>
  416. kwarg; otherwise <a class="reference internal" href="#json.JSONDecoder" title="json.JSONDecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">JSONDecoder</span></code></a> is used. Additional keyword arguments
  417. will be passed to the constructor of the class.</p>
  418. <p>If the data being deserialized is not a valid JSON document, a
  419. <a class="reference internal" href="#json.JSONDecodeError" title="json.JSONDecodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">JSONDecodeError</span></code></a> will be raised.</p>
  420. <div class="versionchanged">
  421. <p><span class="versionmodified changed">Changed in version 3.6: </span>All optional parameters are now <a class="reference internal" href="../glossary.html#keyword-only-parameter"><span class="std std-ref">keyword-only</span></a>.</p>
  422. </div>
  423. <div class="versionchanged">
  424. <p><span class="versionmodified changed">Changed in version 3.6: </span><em>fp</em> can now be a <a class="reference internal" href="../glossary.html#term-binary-file"><span class="xref std std-term">binary file</span></a>. The input encoding should be
  425. UTF-8, UTF-16 or UTF-32.</p>
  426. </div>
  427. </dd></dl>
  428. <dl class="py function">
  429. <dt class="sig sig-object py" id="json.loads">
  430. <span class="sig-prename descclassname"><span class="pre">json.</span></span><span class="sig-name descname"><span class="pre">loads</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">cls</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">object_hook</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parse_float</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parse_int</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parse_constant</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">object_pairs_hook</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kw</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json.loads" title="Link to this definition">¶</a></dt>
  431. <dd><p>Deserialize <em>s</em> (a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>, <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> or <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a>
  432. instance containing a JSON document) to a Python object using this
  433. <a class="reference internal" href="#json-to-py-table"><span class="std std-ref">conversion table</span></a>.</p>
  434. <p>The other arguments have the same meaning as in <a class="reference internal" href="#json.load" title="json.load"><code class="xref py py-func docutils literal notranslate"><span class="pre">load()</span></code></a>.</p>
  435. <p>If the data being deserialized is not a valid JSON document, a
  436. <a class="reference internal" href="#json.JSONDecodeError" title="json.JSONDecodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">JSONDecodeError</span></code></a> will be raised.</p>
  437. <div class="versionchanged">
  438. <p><span class="versionmodified changed">Changed in version 3.6: </span><em>s</em> can now be of type <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> or <a class="reference internal" href="stdtypes.html#bytearray" title="bytearray"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytearray</span></code></a>. The
  439. input encoding should be UTF-8, UTF-16 or UTF-32.</p>
  440. </div>
  441. <div class="versionchanged">
  442. <p><span class="versionmodified changed">Changed in version 3.9: </span>The keyword argument <em>encoding</em> has been removed.</p>
  443. </div>
  444. </dd></dl>
  445. </section>
  446. <section id="encoders-and-decoders">
  447. <h2>Encoders and Decoders<a class="headerlink" href="#encoders-and-decoders" title="Link to this heading">¶</a></h2>
  448. <dl class="py class">
  449. <dt class="sig sig-object py" id="json.JSONDecoder">
  450. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">json.</span></span><span class="sig-name descname"><span class="pre">JSONDecoder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">object_hook</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parse_float</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parse_int</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">parse_constant</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">strict</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">object_pairs_hook</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json.JSONDecoder" title="Link to this definition">¶</a></dt>
  451. <dd><p>Simple JSON decoder.</p>
  452. <p>Performs the following translations in decoding by default:</p>
  453. <table class="docutils align-default" id="json-to-py-table">
  454. <thead>
  455. <tr class="row-odd"><th class="head"><p>JSON</p></th>
  456. <th class="head"><p>Python</p></th>
  457. </tr>
  458. </thead>
  459. <tbody>
  460. <tr class="row-even"><td><p>object</p></td>
  461. <td><p>dict</p></td>
  462. </tr>
  463. <tr class="row-odd"><td><p>array</p></td>
  464. <td><p>list</p></td>
  465. </tr>
  466. <tr class="row-even"><td><p>string</p></td>
  467. <td><p>str</p></td>
  468. </tr>
  469. <tr class="row-odd"><td><p>number (int)</p></td>
  470. <td><p>int</p></td>
  471. </tr>
  472. <tr class="row-even"><td><p>number (real)</p></td>
  473. <td><p>float</p></td>
  474. </tr>
  475. <tr class="row-odd"><td><p>true</p></td>
  476. <td><p>True</p></td>
  477. </tr>
  478. <tr class="row-even"><td><p>false</p></td>
  479. <td><p>False</p></td>
  480. </tr>
  481. <tr class="row-odd"><td><p>null</p></td>
  482. <td><p>None</p></td>
  483. </tr>
  484. </tbody>
  485. </table>
  486. <p>It also understands <code class="docutils literal notranslate"><span class="pre">NaN</span></code>, <code class="docutils literal notranslate"><span class="pre">Infinity</span></code>, and <code class="docutils literal notranslate"><span class="pre">-Infinity</span></code> as their
  487. corresponding <code class="docutils literal notranslate"><span class="pre">float</span></code> values, which is outside the JSON spec.</p>
  488. <p><em>object_hook</em>, if specified, will be called with the result of every JSON
  489. object decoded and its return value will be used in place of the given
  490. <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>. This can be used to provide custom deserializations (e.g. to
  491. support <a class="reference external" href="https://www.jsonrpc.org">JSON-RPC</a> class hinting).</p>
  492. <p><em>object_pairs_hook</em>, if specified will be called with the result of every
  493. JSON object decoded with an ordered list of pairs. The return value of
  494. <em>object_pairs_hook</em> will be used instead of the <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>. This
  495. feature can be used to implement custom decoders. If <em>object_hook</em> is also
  496. defined, the <em>object_pairs_hook</em> takes priority.</p>
  497. <div class="versionchanged">
  498. <p><span class="versionmodified changed">Changed in version 3.1: </span>Added support for <em>object_pairs_hook</em>.</p>
  499. </div>
  500. <p><em>parse_float</em>, if specified, will be called with the string of every JSON
  501. float to be decoded. By default, this is equivalent to <code class="docutils literal notranslate"><span class="pre">float(num_str)</span></code>.
  502. This can be used to use another datatype or parser for JSON floats
  503. (e.g. <a class="reference internal" href="decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">decimal.Decimal</span></code></a>).</p>
  504. <p><em>parse_int</em>, if specified, will be called with the string of every JSON int
  505. to be decoded. By default, this is equivalent to <code class="docutils literal notranslate"><span class="pre">int(num_str)</span></code>. This can
  506. be used to use another datatype or parser for JSON integers
  507. (e.g. <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>).</p>
  508. <p><em>parse_constant</em>, if specified, will be called with one of the following
  509. strings: <code class="docutils literal notranslate"><span class="pre">'-Infinity'</span></code>, <code class="docutils literal notranslate"><span class="pre">'Infinity'</span></code>, <code class="docutils literal notranslate"><span class="pre">'NaN'</span></code>.
  510. This can be used to raise an exception if invalid JSON numbers
  511. are encountered.</p>
  512. <p>If <em>strict</em> is false (<code class="docutils literal notranslate"><span class="pre">True</span></code> is the default), then control characters
  513. will be allowed inside strings. Control characters in this context are
  514. those with character codes in the 0–31 range, including <code class="docutils literal notranslate"><span class="pre">'\t'</span></code> (tab),
  515. <code class="docutils literal notranslate"><span class="pre">'\n'</span></code>, <code class="docutils literal notranslate"><span class="pre">'\r'</span></code> and <code class="docutils literal notranslate"><span class="pre">'\0'</span></code>.</p>
  516. <p>If the data being deserialized is not a valid JSON document, a
  517. <a class="reference internal" href="#json.JSONDecodeError" title="json.JSONDecodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">JSONDecodeError</span></code></a> will be raised.</p>
  518. <div class="versionchanged">
  519. <p><span class="versionmodified changed">Changed in version 3.6: </span>All parameters are now <a class="reference internal" href="../glossary.html#keyword-only-parameter"><span class="std std-ref">keyword-only</span></a>.</p>
  520. </div>
  521. <dl class="py method">
  522. <dt class="sig sig-object py" id="json.JSONDecoder.decode">
  523. <span class="sig-name descname"><span class="pre">decode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json.JSONDecoder.decode" title="Link to this definition">¶</a></dt>
  524. <dd><p>Return the Python representation of <em>s</em> (a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> instance
  525. containing a JSON document).</p>
  526. <p><a class="reference internal" href="#json.JSONDecodeError" title="json.JSONDecodeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">JSONDecodeError</span></code></a> will be raised if the given JSON document is not
  527. valid.</p>
  528. </dd></dl>
  529. <dl class="py method">
  530. <dt class="sig sig-object py" id="json.JSONDecoder.raw_decode">
  531. <span class="sig-name descname"><span class="pre">raw_decode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">s</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json.JSONDecoder.raw_decode" title="Link to this definition">¶</a></dt>
  532. <dd><p>Decode a JSON document from <em>s</em> (a <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> beginning with a
  533. JSON document) and return a 2-tuple of the Python representation
  534. and the index in <em>s</em> where the document ended.</p>
  535. <p>This can be used to decode a JSON document from a string that may have
  536. extraneous data at the end.</p>
  537. </dd></dl>
  538. </dd></dl>
  539. <dl class="py class">
  540. <dt class="sig sig-object py" id="json.JSONEncoder">
  541. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">json.</span></span><span class="sig-name descname"><span class="pre">JSONEncoder</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">skipkeys</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">ensure_ascii</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">check_circular</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">allow_nan</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sort_keys</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">indent</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">separators</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json.JSONEncoder" title="Link to this definition">¶</a></dt>
  542. <dd><p>Extensible JSON encoder for Python data structures.</p>
  543. <p>Supports the following objects and types by default:</p>
  544. <table class="docutils align-default" id="py-to-json-table">
  545. <thead>
  546. <tr class="row-odd"><th class="head"><p>Python</p></th>
  547. <th class="head"><p>JSON</p></th>
  548. </tr>
  549. </thead>
  550. <tbody>
  551. <tr class="row-even"><td><p>dict</p></td>
  552. <td><p>object</p></td>
  553. </tr>
  554. <tr class="row-odd"><td><p>list, tuple</p></td>
  555. <td><p>array</p></td>
  556. </tr>
  557. <tr class="row-even"><td><p>str</p></td>
  558. <td><p>string</p></td>
  559. </tr>
  560. <tr class="row-odd"><td><p>int, float, int- &amp; float-derived Enums</p></td>
  561. <td><p>number</p></td>
  562. </tr>
  563. <tr class="row-even"><td><p>True</p></td>
  564. <td><p>true</p></td>
  565. </tr>
  566. <tr class="row-odd"><td><p>False</p></td>
  567. <td><p>false</p></td>
  568. </tr>
  569. <tr class="row-even"><td><p>None</p></td>
  570. <td><p>null</p></td>
  571. </tr>
  572. </tbody>
  573. </table>
  574. <div class="versionchanged">
  575. <p><span class="versionmodified changed">Changed in version 3.4: </span>Added support for int- and float-derived Enum classes.</p>
  576. </div>
  577. <p>To extend this to recognize other objects, subclass and implement a
  578. <a class="reference internal" href="#json.JSONEncoder.default" title="json.JSONEncoder.default"><code class="xref py py-meth docutils literal notranslate"><span class="pre">default()</span></code></a> method with another method that returns a serializable object
  579. for <code class="docutils literal notranslate"><span class="pre">o</span></code> if possible, otherwise it should call the superclass implementation
  580. (to raise <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>).</p>
  581. <p>If <em>skipkeys</em> is false (the default), a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> will be raised when
  582. trying to encode keys that are not <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>, <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a>, <a class="reference internal" href="functions.html#float" title="float"><code class="xref py py-class docutils literal notranslate"><span class="pre">float</span></code></a>
  583. or <code class="docutils literal notranslate"><span class="pre">None</span></code>. If <em>skipkeys</em> is true, such items are simply skipped.</p>
  584. <p>If <em>ensure_ascii</em> is true (the default), the output is guaranteed to
  585. have all incoming non-ASCII characters escaped. If <em>ensure_ascii</em> is
  586. false, these characters will be output as-is.</p>
  587. <p>If <em>check_circular</em> is true (the default), then lists, dicts, and custom
  588. encoded objects will be checked for circular references during encoding to
  589. prevent an infinite recursion (which would cause a <a class="reference internal" href="exceptions.html#RecursionError" title="RecursionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RecursionError</span></code></a>).
  590. Otherwise, no such check takes place.</p>
  591. <p>If <em>allow_nan</em> is true (the default), then <code class="docutils literal notranslate"><span class="pre">NaN</span></code>, <code class="docutils literal notranslate"><span class="pre">Infinity</span></code>, and
  592. <code class="docutils literal notranslate"><span class="pre">-Infinity</span></code> will be encoded as such. This behavior is not JSON
  593. specification compliant, but is consistent with most JavaScript based
  594. encoders and decoders. Otherwise, it will be a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> to encode
  595. such floats.</p>
  596. <p>If <em>sort_keys</em> is true (default: <code class="docutils literal notranslate"><span class="pre">False</span></code>), then the output of dictionaries
  597. will be sorted by key; this is useful for regression tests to ensure that
  598. JSON serializations can be compared on a day-to-day basis.</p>
  599. <p>If <em>indent</em> is a non-negative integer or string, then JSON array elements and
  600. object members will be pretty-printed with that indent level. An indent level
  601. of 0, negative, or <code class="docutils literal notranslate"><span class="pre">&quot;&quot;</span></code> will only insert newlines. <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default)
  602. selects the most compact representation. Using a positive integer indent
  603. indents that many spaces per level. If <em>indent</em> is a string (such as <code class="docutils literal notranslate"><span class="pre">&quot;\t&quot;</span></code>),
  604. that string is used to indent each level.</p>
  605. <div class="versionchanged">
  606. <p><span class="versionmodified changed">Changed in version 3.2: </span>Allow strings for <em>indent</em> in addition to integers.</p>
  607. </div>
  608. <p>If specified, <em>separators</em> should be an <code class="docutils literal notranslate"><span class="pre">(item_separator,</span> <span class="pre">key_separator)</span></code>
  609. tuple. The default is <code class="docutils literal notranslate"><span class="pre">(',</span> <span class="pre">',</span> <span class="pre">':</span> <span class="pre">')</span></code> if <em>indent</em> is <code class="docutils literal notranslate"><span class="pre">None</span></code> and
  610. <code class="docutils literal notranslate"><span class="pre">(',',</span> <span class="pre">':</span> <span class="pre">')</span></code> otherwise. To get the most compact JSON representation,
  611. you should specify <code class="docutils literal notranslate"><span class="pre">(',',</span> <span class="pre">':')</span></code> to eliminate whitespace.</p>
  612. <div class="versionchanged">
  613. <p><span class="versionmodified changed">Changed in version 3.4: </span>Use <code class="docutils literal notranslate"><span class="pre">(',',</span> <span class="pre">':</span> <span class="pre">')</span></code> as default if <em>indent</em> is not <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
  614. </div>
  615. <p>If specified, <em>default</em> should be a function that gets called for objects that
  616. can’t otherwise be serialized. It should return a JSON encodable version of
  617. the object or raise a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>. If not specified, <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>
  618. is raised.</p>
  619. <div class="versionchanged">
  620. <p><span class="versionmodified changed">Changed in version 3.6: </span>All parameters are now <a class="reference internal" href="../glossary.html#keyword-only-parameter"><span class="std std-ref">keyword-only</span></a>.</p>
  621. </div>
  622. <dl class="py method">
  623. <dt class="sig sig-object py" id="json.JSONEncoder.default">
  624. <span class="sig-name descname"><span class="pre">default</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">o</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json.JSONEncoder.default" title="Link to this definition">¶</a></dt>
  625. <dd><p>Implement this method in a subclass such that it returns a serializable
  626. object for <em>o</em>, or calls the base implementation (to raise a
  627. <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>).</p>
  628. <p>For example, to support arbitrary iterators, you could implement
  629. <a class="reference internal" href="#json.JSONEncoder.default" title="json.JSONEncoder.default"><code class="xref py py-meth docutils literal notranslate"><span class="pre">default()</span></code></a> like this:</p>
  630. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">default</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">o</span><span class="p">):</span>
  631. <span class="k">try</span><span class="p">:</span>
  632. <span class="n">iterable</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">o</span><span class="p">)</span>
  633. <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
  634. <span class="k">pass</span>
  635. <span class="k">else</span><span class="p">:</span>
  636. <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span>
  637. <span class="c1"># Let the base class default method raise the TypeError</span>
  638. <span class="k">return</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">default</span><span class="p">(</span><span class="n">o</span><span class="p">)</span>
  639. </pre></div>
  640. </div>
  641. </dd></dl>
  642. <dl class="py method">
  643. <dt class="sig sig-object py" id="json.JSONEncoder.encode">
  644. <span class="sig-name descname"><span class="pre">encode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">o</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json.JSONEncoder.encode" title="Link to this definition">¶</a></dt>
  645. <dd><p>Return a JSON string representation of a Python data structure, <em>o</em>. For
  646. example:</p>
  647. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">JSONEncoder</span><span class="p">()</span><span class="o">.</span><span class="n">encode</span><span class="p">({</span><span class="s2">&quot;foo&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;bar&quot;</span><span class="p">,</span> <span class="s2">&quot;baz&quot;</span><span class="p">]})</span>
  648. <span class="go">&#39;{&quot;foo&quot;: [&quot;bar&quot;, &quot;baz&quot;]}&#39;</span>
  649. </pre></div>
  650. </div>
  651. </dd></dl>
  652. <dl class="py method">
  653. <dt class="sig sig-object py" id="json.JSONEncoder.iterencode">
  654. <span class="sig-name descname"><span class="pre">iterencode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">o</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json.JSONEncoder.iterencode" title="Link to this definition">¶</a></dt>
  655. <dd><p>Encode the given object, <em>o</em>, and yield each string representation as
  656. available. For example:</p>
  657. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">chunk</span> <span class="ow">in</span> <span class="n">json</span><span class="o">.</span><span class="n">JSONEncoder</span><span class="p">()</span><span class="o">.</span><span class="n">iterencode</span><span class="p">(</span><span class="n">bigobject</span><span class="p">):</span>
  658. <span class="n">mysocket</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">chunk</span><span class="p">)</span>
  659. </pre></div>
  660. </div>
  661. </dd></dl>
  662. </dd></dl>
  663. </section>
  664. <section id="exceptions">
  665. <h2>Exceptions<a class="headerlink" href="#exceptions" title="Link to this heading">¶</a></h2>
  666. <dl class="py exception">
  667. <dt class="sig sig-object py" id="json.JSONDecodeError">
  668. <em class="property"><span class="pre">exception</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">json.</span></span><span class="sig-name descname"><span class="pre">JSONDecodeError</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">msg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">doc</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">pos</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#json.JSONDecodeError" title="Link to this definition">¶</a></dt>
  669. <dd><p>Subclass of <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> with the following additional attributes:</p>
  670. <dl class="py attribute">
  671. <dt class="sig sig-object py" id="json.JSONDecodeError.msg">
  672. <span class="sig-name descname"><span class="pre">msg</span></span><a class="headerlink" href="#json.JSONDecodeError.msg" title="Link to this definition">¶</a></dt>
  673. <dd><p>The unformatted error message.</p>
  674. </dd></dl>
  675. <dl class="py attribute">
  676. <dt class="sig sig-object py" id="json.JSONDecodeError.doc">
  677. <span class="sig-name descname"><span class="pre">doc</span></span><a class="headerlink" href="#json.JSONDecodeError.doc" title="Link to this definition">¶</a></dt>
  678. <dd><p>The JSON document being parsed.</p>
  679. </dd></dl>
  680. <dl class="py attribute">
  681. <dt class="sig sig-object py" id="json.JSONDecodeError.pos">
  682. <span class="sig-name descname"><span class="pre">pos</span></span><a class="headerlink" href="#json.JSONDecodeError.pos" title="Link to this definition">¶</a></dt>
  683. <dd><p>The start index of <em>doc</em> where parsing failed.</p>
  684. </dd></dl>
  685. <dl class="py attribute">
  686. <dt class="sig sig-object py" id="json.JSONDecodeError.lineno">
  687. <span class="sig-name descname"><span class="pre">lineno</span></span><a class="headerlink" href="#json.JSONDecodeError.lineno" title="Link to this definition">¶</a></dt>
  688. <dd><p>The line corresponding to <em>pos</em>.</p>
  689. </dd></dl>
  690. <dl class="py attribute">
  691. <dt class="sig sig-object py" id="json.JSONDecodeError.colno">
  692. <span class="sig-name descname"><span class="pre">colno</span></span><a class="headerlink" href="#json.JSONDecodeError.colno" title="Link to this definition">¶</a></dt>
  693. <dd><p>The column corresponding to <em>pos</em>.</p>
  694. </dd></dl>
  695. <div class="versionadded">
  696. <p><span class="versionmodified added">New in version 3.5.</span></p>
  697. </div>
  698. </dd></dl>
  699. </section>
  700. <section id="standard-compliance-and-interoperability">
  701. <h2>Standard Compliance and Interoperability<a class="headerlink" href="#standard-compliance-and-interoperability" title="Link to this heading">¶</a></h2>
  702. <p>The JSON format is specified by <span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc7159.html"><strong>RFC 7159</strong></a> and by
  703. <a class="reference external" href="https://www.ecma-international.org/publications-and-standards/standards/ecma-404/">ECMA-404</a>.
  704. This section details this module’s level of compliance with the RFC.
  705. For simplicity, <a class="reference internal" href="#json.JSONEncoder" title="json.JSONEncoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">JSONEncoder</span></code></a> and <a class="reference internal" href="#json.JSONDecoder" title="json.JSONDecoder"><code class="xref py py-class docutils literal notranslate"><span class="pre">JSONDecoder</span></code></a> subclasses, and
  706. parameters other than those explicitly mentioned, are not considered.</p>
  707. <p>This module does not comply with the RFC in a strict fashion, implementing some
  708. extensions that are valid JavaScript but not valid JSON. In particular:</p>
  709. <ul class="simple">
  710. <li><p>Infinite and NaN number values are accepted and output;</p></li>
  711. <li><p>Repeated names within an object are accepted, and only the value of the last
  712. name-value pair is used.</p></li>
  713. </ul>
  714. <p>Since the RFC permits RFC-compliant parsers to accept input texts that are not
  715. RFC-compliant, this module’s deserializer is technically RFC-compliant under
  716. default settings.</p>
  717. <section id="character-encodings">
  718. <h3>Character Encodings<a class="headerlink" href="#character-encodings" title="Link to this heading">¶</a></h3>
  719. <p>The RFC requires that JSON be represented using either UTF-8, UTF-16, or
  720. UTF-32, with UTF-8 being the recommended default for maximum interoperability.</p>
  721. <p>As permitted, though not required, by the RFC, this module’s serializer sets
  722. <em>ensure_ascii=True</em> by default, thus escaping the output so that the resulting
  723. strings only contain ASCII characters.</p>
  724. <p>Other than the <em>ensure_ascii</em> parameter, this module is defined strictly in
  725. terms of conversion between Python objects and
  726. <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">Unicode</span> <span class="pre">strings</span></code></a>, and thus does not otherwise directly address
  727. the issue of character encodings.</p>
  728. <p>The RFC prohibits adding a byte order mark (BOM) to the start of a JSON text,
  729. and this module’s serializer does not add a BOM to its output.
  730. The RFC permits, but does not require, JSON deserializers to ignore an initial
  731. BOM in their input. This module’s deserializer raises a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a>
  732. when an initial BOM is present.</p>
  733. <p>The RFC does not explicitly forbid JSON strings which contain byte sequences
  734. that don’t correspond to valid Unicode characters (e.g. unpaired UTF-16
  735. surrogates), but it does note that they may cause interoperability problems.
  736. By default, this module accepts and outputs (when present in the original
  737. <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>) code points for such sequences.</p>
  738. </section>
  739. <section id="infinite-and-nan-number-values">
  740. <h3>Infinite and NaN Number Values<a class="headerlink" href="#infinite-and-nan-number-values" title="Link to this heading">¶</a></h3>
  741. <p>The RFC does not permit the representation of infinite or NaN number values.
  742. Despite that, by default, this module accepts and outputs <code class="docutils literal notranslate"><span class="pre">Infinity</span></code>,
  743. <code class="docutils literal notranslate"><span class="pre">-Infinity</span></code>, and <code class="docutils literal notranslate"><span class="pre">NaN</span></code> as if they were valid JSON number literal values:</p>
  744. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># Neither of these calls raises an exception, but the results are not valid JSON</span>
  745. <span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="s1">&#39;-inf&#39;</span><span class="p">))</span>
  746. <span class="go">&#39;-Infinity&#39;</span>
  747. <span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="s1">&#39;nan&#39;</span><span class="p">))</span>
  748. <span class="go">&#39;NaN&#39;</span>
  749. <span class="gp">&gt;&gt;&gt; </span><span class="c1"># Same when deserializing</span>
  750. <span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="s1">&#39;-Infinity&#39;</span><span class="p">)</span>
  751. <span class="go">-inf</span>
  752. <span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="s1">&#39;NaN&#39;</span><span class="p">)</span>
  753. <span class="go">nan</span>
  754. </pre></div>
  755. </div>
  756. <p>In the serializer, the <em>allow_nan</em> parameter can be used to alter this
  757. behavior. In the deserializer, the <em>parse_constant</em> parameter can be used to
  758. alter this behavior.</p>
  759. </section>
  760. <section id="repeated-names-within-an-object">
  761. <h3>Repeated Names Within an Object<a class="headerlink" href="#repeated-names-within-an-object" title="Link to this heading">¶</a></h3>
  762. <p>The RFC specifies that the names within a JSON object should be unique, but
  763. does not mandate how repeated names in JSON objects should be handled. By
  764. default, this module does not raise an exception; instead, it ignores all but
  765. the last name-value pair for a given name:</p>
  766. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">weird_json</span> <span class="o">=</span> <span class="s1">&#39;{&quot;x&quot;: 1, &quot;x&quot;: 2, &quot;x&quot;: 3}&#39;</span>
  767. <span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">weird_json</span><span class="p">)</span>
  768. <span class="go">{&#39;x&#39;: 3}</span>
  769. </pre></div>
  770. </div>
  771. <p>The <em>object_pairs_hook</em> parameter can be used to alter this behavior.</p>
  772. </section>
  773. <section id="top-level-non-object-non-array-values">
  774. <h3>Top-level Non-Object, Non-Array Values<a class="headerlink" href="#top-level-non-object-non-array-values" title="Link to this heading">¶</a></h3>
  775. <p>The old version of JSON specified by the obsolete <span class="target" id="index-3"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc4627.html"><strong>RFC 4627</strong></a> required that
  776. the top-level value of a JSON text must be either a JSON object or array
  777. (Python <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> or <a class="reference internal" href="stdtypes.html#list" title="list"><code class="xref py py-class docutils literal notranslate"><span class="pre">list</span></code></a>), and could not be a JSON null,
  778. boolean, number, or string value. <span class="target" id="index-4"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc7159.html"><strong>RFC 7159</strong></a> removed that restriction, and
  779. this module does not and has never implemented that restriction in either its
  780. serializer or its deserializer.</p>
  781. <p>Regardless, for maximum interoperability, you may wish to voluntarily adhere
  782. to the restriction yourself.</p>
  783. </section>
  784. <section id="implementation-limitations">
  785. <h3>Implementation Limitations<a class="headerlink" href="#implementation-limitations" title="Link to this heading">¶</a></h3>
  786. <p>Some JSON deserializer implementations may set limits on:</p>
  787. <ul class="simple">
  788. <li><p>the size of accepted JSON texts</p></li>
  789. <li><p>the maximum level of nesting of JSON objects and arrays</p></li>
  790. <li><p>the range and precision of JSON numbers</p></li>
  791. <li><p>the content and maximum length of JSON strings</p></li>
  792. </ul>
  793. <p>This module does not impose any such limits beyond those of the relevant
  794. Python datatypes themselves or the Python interpreter itself.</p>
  795. <p>When serializing to JSON, beware any such limitations in applications that may
  796. consume your JSON. In particular, it is common for JSON numbers to be
  797. deserialized into IEEE 754 double precision numbers and thus subject to that
  798. representation’s range and precision limitations. This is especially relevant
  799. when serializing Python <a class="reference internal" href="functions.html#int" title="int"><code class="xref py py-class docutils literal notranslate"><span class="pre">int</span></code></a> values of extremely large magnitude, or
  800. when serializing instances of “exotic” numerical types such as
  801. <a class="reference internal" href="decimal.html#decimal.Decimal" title="decimal.Decimal"><code class="xref py py-class docutils literal notranslate"><span class="pre">decimal.Decimal</span></code></a>.</p>
  802. </section>
  803. </section>
  804. <section id="module-json.tool">
  805. <span id="command-line-interface"></span><span id="json-commandline"></span><h2>Command Line Interface<a class="headerlink" href="#module-json.tool" title="Link to this heading">¶</a></h2>
  806. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/json/tool.py">Lib/json/tool.py</a></p>
  807. <hr class="docutils" />
  808. <p>The <a class="reference internal" href="#module-json.tool" title="json.tool: A command line to validate and pretty-print JSON."><code class="xref py py-mod docutils literal notranslate"><span class="pre">json.tool</span></code></a> module provides a simple command line interface to validate
  809. and pretty-print JSON objects.</p>
  810. <p>If the optional <code class="docutils literal notranslate"><span class="pre">infile</span></code> and <code class="docutils literal notranslate"><span class="pre">outfile</span></code> arguments are not
  811. specified, <a class="reference internal" href="sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a> and <a class="reference internal" href="sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a> will be used respectively:</p>
  812. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span><span class="nb">echo</span><span class="w"> </span><span class="s1">&#39;{&quot;json&quot;: &quot;obj&quot;}&#39;</span><span class="w"> </span><span class="p">|</span><span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>json.tool
  813. <span class="go">{</span>
  814. <span class="go"> &quot;json&quot;: &quot;obj&quot;</span>
  815. <span class="go">}</span>
  816. <span class="gp">$ </span><span class="nb">echo</span><span class="w"> </span><span class="s1">&#39;{1.2:3.4}&#39;</span><span class="w"> </span><span class="p">|</span><span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>json.tool
  817. <span class="go">Expecting property name enclosed in double quotes: line 1 column 2 (char 1)</span>
  818. </pre></div>
  819. </div>
  820. <div class="versionchanged">
  821. <p><span class="versionmodified changed">Changed in version 3.5: </span>The output is now in the same order as the input. Use the
  822. <a class="reference internal" href="#cmdoption-json.tool-sort-keys"><code class="xref std std-option docutils literal notranslate"><span class="pre">--sort-keys</span></code></a> option to sort the output of dictionaries
  823. alphabetically by key.</p>
  824. </div>
  825. <section id="command-line-options">
  826. <h3>Command line options<a class="headerlink" href="#command-line-options" title="Link to this heading">¶</a></h3>
  827. <dl class="std option">
  828. <dt class="sig sig-object std" id="cmdoption-json.tool-arg-infile">
  829. <span class="sig-name descname"><span class="pre">infile</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-json.tool-arg-infile" title="Link to this definition">¶</a></dt>
  830. <dd><p>The JSON file to be validated or pretty-printed:</p>
  831. <div class="highlight-shell-session notranslate"><div class="highlight"><pre><span></span><span class="gp">$ </span>python<span class="w"> </span>-m<span class="w"> </span>json.tool<span class="w"> </span>mp_films.json
  832. <span class="go">[</span>
  833. <span class="go"> {</span>
  834. <span class="go"> &quot;title&quot;: &quot;And Now for Something Completely Different&quot;,</span>
  835. <span class="go"> &quot;year&quot;: 1971</span>
  836. <span class="go"> },</span>
  837. <span class="go"> {</span>
  838. <span class="go"> &quot;title&quot;: &quot;Monty Python and the Holy Grail&quot;,</span>
  839. <span class="go"> &quot;year&quot;: 1975</span>
  840. <span class="go"> }</span>
  841. <span class="go">]</span>
  842. </pre></div>
  843. </div>
  844. <p>If <em>infile</em> is not specified, read from <a class="reference internal" href="sys.html#sys.stdin" title="sys.stdin"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdin</span></code></a>.</p>
  845. </dd></dl>
  846. <dl class="std option">
  847. <dt class="sig sig-object std" id="cmdoption-json.tool-arg-outfile">
  848. <span class="sig-name descname"><span class="pre">outfile</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-json.tool-arg-outfile" title="Link to this definition">¶</a></dt>
  849. <dd><p>Write the output of the <em>infile</em> to the given <em>outfile</em>. Otherwise, write it
  850. to <a class="reference internal" href="sys.html#sys.stdout" title="sys.stdout"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.stdout</span></code></a>.</p>
  851. </dd></dl>
  852. <dl class="std option">
  853. <dt class="sig sig-object std" id="cmdoption-json.tool-sort-keys">
  854. <span class="sig-name descname"><span class="pre">--sort-keys</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-json.tool-sort-keys" title="Link to this definition">¶</a></dt>
  855. <dd><p>Sort the output of dictionaries alphabetically by key.</p>
  856. <div class="versionadded">
  857. <p><span class="versionmodified added">New in version 3.5.</span></p>
  858. </div>
  859. </dd></dl>
  860. <dl class="std option">
  861. <dt class="sig sig-object std" id="cmdoption-json.tool-no-ensure-ascii">
  862. <span class="sig-name descname"><span class="pre">--no-ensure-ascii</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-json.tool-no-ensure-ascii" title="Link to this definition">¶</a></dt>
  863. <dd><p>Disable escaping of non-ascii characters, see <a class="reference internal" href="#json.dumps" title="json.dumps"><code class="xref py py-func docutils literal notranslate"><span class="pre">json.dumps()</span></code></a> for more information.</p>
  864. <div class="versionadded">
  865. <p><span class="versionmodified added">New in version 3.9.</span></p>
  866. </div>
  867. </dd></dl>
  868. <dl class="std option">
  869. <dt class="sig sig-object std" id="cmdoption-json.tool-json-lines">
  870. <span class="sig-name descname"><span class="pre">--json-lines</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-json.tool-json-lines" title="Link to this definition">¶</a></dt>
  871. <dd><p>Parse every input line as separate JSON object.</p>
  872. <div class="versionadded">
  873. <p><span class="versionmodified added">New in version 3.8.</span></p>
  874. </div>
  875. </dd></dl>
  876. <dl class="std option">
  877. <dt class="sig sig-object std" id="cmdoption-json.tool-indent">
  878. <span id="cmdoption-json.tool-tab"></span><span id="cmdoption-json.tool-no-indent"></span><span id="cmdoption-json.tool-compact"></span><span class="sig-name descname"><span class="pre">--indent</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--tab</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--no-indent</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--compact</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-json.tool-indent" title="Link to this definition">¶</a></dt>
  879. <dd><p>Mutually exclusive options for whitespace control.</p>
  880. <div class="versionadded">
  881. <p><span class="versionmodified added">New in version 3.9.</span></p>
  882. </div>
  883. </dd></dl>
  884. <dl class="std option">
  885. <dt class="sig sig-object std" id="cmdoption-json.tool-h">
  886. <span id="cmdoption-json.tool-help"></span><span class="sig-name descname"><span class="pre">-h</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--help</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-json.tool-h" title="Link to this definition">¶</a></dt>
  887. <dd><p>Show the help message.</p>
  888. </dd></dl>
  889. <p class="rubric">Footnotes</p>
  890. <aside class="footnote-list brackets">
  891. <aside class="footnote brackets" id="rfc-errata" role="doc-footnote">
  892. <span class="label"><span class="fn-bracket">[</span><a role="doc-backlink" href="#id1">1</a><span class="fn-bracket">]</span></span>
  893. <p>As noted in <a class="reference external" href="https://www.rfc-editor.org/errata_search.php?rfc=7159">the errata for RFC 7159</a>,
  894. JSON permits literal U+2028 (LINE SEPARATOR) and
  895. U+2029 (PARAGRAPH SEPARATOR) characters in strings, whereas JavaScript
  896. (as of ECMAScript Edition 5.1) does not.</p>
  897. </aside>
  898. </aside>
  899. </section>
  900. </section>
  901. </section>
  902. <div class="clearer"></div>
  903. </div>
  904. </div>
  905. </div>
  906. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  907. <div class="sphinxsidebarwrapper">
  908. <div>
  909. <h3><a href="../contents.html">Table of Contents</a></h3>
  910. <ul>
  911. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code> — JSON encoder and decoder</a><ul>
  912. <li><a class="reference internal" href="#basic-usage">Basic Usage</a></li>
  913. <li><a class="reference internal" href="#encoders-and-decoders">Encoders and Decoders</a></li>
  914. <li><a class="reference internal" href="#exceptions">Exceptions</a></li>
  915. <li><a class="reference internal" href="#standard-compliance-and-interoperability">Standard Compliance and Interoperability</a><ul>
  916. <li><a class="reference internal" href="#character-encodings">Character Encodings</a></li>
  917. <li><a class="reference internal" href="#infinite-and-nan-number-values">Infinite and NaN Number Values</a></li>
  918. <li><a class="reference internal" href="#repeated-names-within-an-object">Repeated Names Within an Object</a></li>
  919. <li><a class="reference internal" href="#top-level-non-object-non-array-values">Top-level Non-Object, Non-Array Values</a></li>
  920. <li><a class="reference internal" href="#implementation-limitations">Implementation Limitations</a></li>
  921. </ul>
  922. </li>
  923. <li><a class="reference internal" href="#module-json.tool">Command Line Interface</a><ul>
  924. <li><a class="reference internal" href="#command-line-options">Command line options</a></li>
  925. </ul>
  926. </li>
  927. </ul>
  928. </li>
  929. </ul>
  930. </div>
  931. <div>
  932. <h4>Previous topic</h4>
  933. <p class="topless"><a href="email.iterators.html"
  934. title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">email.iterators</span></code>: Iterators</a></p>
  935. </div>
  936. <div>
  937. <h4>Next topic</h4>
  938. <p class="topless"><a href="mailbox.html"
  939. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">mailbox</span></code> — Manipulate mailboxes in various formats</a></p>
  940. </div>
  941. <div role="note" aria-label="source link">
  942. <h3>This Page</h3>
  943. <ul class="this-page-menu">
  944. <li><a href="../bugs.html">Report a Bug</a></li>
  945. <li>
  946. <a href="https://github.com/python/cpython/blob/main/Doc/library/json.rst"
  947. rel="nofollow">Show Source
  948. </a>
  949. </li>
  950. </ul>
  951. </div>
  952. </div>
  953. <div id="sidebarbutton" title="Collapse sidebar">
  954. <span>«</span>
  955. </div>
  956. </div>
  957. <div class="clearer"></div>
  958. </div>
  959. <div class="related" role="navigation" aria-label="related navigation">
  960. <h3>Navigation</h3>
  961. <ul>
  962. <li class="right" style="margin-right: 10px">
  963. <a href="../genindex.html" title="General Index"
  964. >index</a></li>
  965. <li class="right" >
  966. <a href="../py-modindex.html" title="Python Module Index"
  967. >modules</a> |</li>
  968. <li class="right" >
  969. <a href="mailbox.html" title="mailbox — Manipulate mailboxes in various formats"
  970. >next</a> |</li>
  971. <li class="right" >
  972. <a href="email.iterators.html" title="email.iterators: Iterators"
  973. >previous</a> |</li>
  974. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  975. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  976. <li class="switchers">
  977. <div class="language_switcher_placeholder"></div>
  978. <div class="version_switcher_placeholder"></div>
  979. </li>
  980. <li>
  981. </li>
  982. <li id="cpython-language-and-version">
  983. <a href="../index.html">3.12.3 Documentation</a> &#187;
  984. </li>
  985. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  986. <li class="nav-item nav-item-2"><a href="netdata.html" >Internet Data Handling</a> &#187;</li>
  987. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">json</span></code> — JSON encoder and decoder</a></li>
  988. <li class="right">
  989. <div class="inline-search" role="search">
  990. <form class="inline-search" action="../search.html" method="get">
  991. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  992. <input type="submit" value="Go" />
  993. </form>
  994. </div>
  995. |
  996. </li>
  997. <li class="right">
  998. <label class="theme-selector-label">
  999. Theme
  1000. <select class="theme-selector" oninput="activateTheme(this.value)">
  1001. <option value="auto" selected>Auto</option>
  1002. <option value="light">Light</option>
  1003. <option value="dark">Dark</option>
  1004. </select>
  1005. </label> |</li>
  1006. </ul>
  1007. </div>
  1008. <div class="footer">
  1009. &copy;
  1010. <a href="../copyright.html">
  1011. Copyright
  1012. </a>
  1013. 2001-2024, Python Software Foundation.
  1014. <br />
  1015. This page is licensed under the Python Software Foundation License Version 2.
  1016. <br />
  1017. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1018. <br />
  1019. See <a href="/license.html">History and License</a> for more information.<br />
  1020. <br />
  1021. The Python Software Foundation is a non-profit corporation.
  1022. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1023. <br />
  1024. <br />
  1025. Last updated on Apr 09, 2024 (13:47 UTC).
  1026. <a href="/bugs.html">Found a bug</a>?
  1027. <br />
  1028. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1029. </div>
  1030. </body>
  1031. </html>
上海开阖软件有限公司 沪ICP备12045867号-1