{"route":"/en-US-v0.14.1/reference/data-loading/json/","title":"JSON","description":"Documentation for the `json` function.","part":null,"outline":[{"id":"summary","name":"Summary","children":[]},{"id":"example","name":"Example","children":[]},{"id":"conversion","name":"Conversion","children":[{"id":"notes","name":"Notes","children":[]}]},{"id":"parameters","name":"Parameters","children":[{"id":"parameters-source","name":"source","children":[]}]},{"id":"definitions","name":"Definitions","children":[{"id":"definitions-decode","name":"Decode JSON","children":[{"id":"definitions-decode-data","name":"data","children":[]}]},{"id":"definitions-encode","name":"Encode JSON","children":[{"id":"definitions-encode-value","name":"value","children":[]},{"id":"definitions-encode-pretty","name":"pretty","children":[]}]}]}],"body":{"kind":"func","content":{"path":[],"name":"json","title":"JSON","keywords":[],"oneliner":"Reads structured data from a JSON file.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Reads structured data from a JSON file.</p>\n<p>The file must contain a valid JSON value, such as object or array. The JSON\nvalues will be converted into corresponding Typst values as listed in the\n<a href=\"#conversion\">table below</a>.</p>\n<p>The function returns a dictionary, an array or, depending on the JSON file,\nanother JSON data type.</p>\n<p>The JSON files in the example contain objects with the keys <code>temperature</code>,\n<code>unit</code>, and <code>weather</code>.</p>\n<h2 id=\"example\">Example</h2>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> <span class=\"typ-func\">forecast</span><span class=\"typ-punct\">(</span>day<span class=\"typ-punct\">)</span> <span class=\"typ-op\">=</span> <span class=\"typ-func\">block</span><span class=\"typ-punct\">[</span>\n  <span class=\"typ-func\">#</span><span class=\"typ-func\">box</span><span class=\"typ-punct\">(</span><span class=\"typ-func\">square</span><span class=\"typ-punct\">(</span>\n    width<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2cm</span><span class=\"typ-punct\">,</span>\n    inset<span class=\"typ-punct\">:</span> <span class=\"typ-num\">8pt</span><span class=\"typ-punct\">,</span>\n    fill<span class=\"typ-punct\">:</span> <span class=\"typ-key\">if</span> day<span class=\"typ-punct\">.</span>weather <span class=\"typ-op\">==</span> <span class=\"typ-str\">&quot;sunny&quot;</span> <span class=\"typ-punct\">{</span>\n      yellow\n    <span class=\"typ-punct\">}</span> <span class=\"typ-key\">else</span> <span class=\"typ-punct\">{</span>\n      aqua\n    <span class=\"typ-punct\">}</span><span class=\"typ-punct\">,</span>\n    <span class=\"typ-func\">align</span><span class=\"typ-punct\">(</span>\n      bottom <span class=\"typ-op\">+</span> right<span class=\"typ-punct\">,</span>\n      <span class=\"typ-func\">strong</span><span class=\"typ-punct\">(</span>day<span class=\"typ-punct\">.</span>weather<span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n    <span class=\"typ-punct\">)</span><span class=\"typ-punct\">,</span>\n  <span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span>\n  <span class=\"typ-func\">#</span><span class=\"typ-func\">h</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">6pt</span><span class=\"typ-punct\">)</span>\n  <span class=\"typ-key\">#</span><span class=\"typ-key\">set</span> <span class=\"typ-func\">text</span><span class=\"typ-punct\">(</span><span class=\"typ-num\">22pt</span><span class=\"typ-punct\">,</span> baseline<span class=\"typ-punct\">:</span> <span class=\"typ-op\">-</span><span class=\"typ-num\">8pt</span><span class=\"typ-punct\">)</span>\n  <span class=\"typ-pol\">#</span><span class=\"typ-pol\">day</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">temperature</span> °<span class=\"typ-pol\">#</span><span class=\"typ-pol\">day</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">unit</span>\n<span class=\"typ-punct\">]</span>\n\n<span class=\"typ-func\">#</span><span class=\"typ-func\">forecast</span><span class=\"typ-punct\">(</span><span class=\"typ-func\">json</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;monday.json&quot;</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-func\">#</span><span class=\"typ-func\">forecast</span><span class=\"typ-punct\">(</span><span class=\"typ-func\">json</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;tuesday.json&quot;</span><span class=\"typ-punct\">)</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/f531864e1bdd9f39c36d5451a39f87b2.png\" alt=\"Preview\"></div></div>\n<h2 id=\"conversion\">Conversion details</h2>\n<table><thead><tr><th>JSON value</th><th>Converted into Typst</th></tr></thead><tbody>\n<tr><td><code>null</code></td><td><code><span class=\"typ-key\">none</span></code></td></tr>\n<tr><td>bool</td><td><a href=\"/en-US-v0.14.1/reference/foundations/bool/\" title=\"`bool`\"><code>bool</code></a></td></tr>\n<tr><td>number</td><td><a href=\"/en-US-v0.14.1/reference/foundations/float/\" title=\"`float`\"><code>float</code></a> or <a href=\"/en-US-v0.14.1/reference/foundations/int/\" title=\"`int`\"><code>int</code></a></td></tr>\n<tr><td>string</td><td><a href=\"/en-US-v0.14.1/reference/foundations/str/\" title=\"`str`\"><code>str</code></a></td></tr>\n<tr><td>array</td><td><a href=\"/en-US-v0.14.1/reference/foundations/array/\" title=\"`array`\"><code>array</code></a></td></tr>\n<tr><td>object</td><td><a href=\"/en-US-v0.14.1/reference/foundations/dictionary/\" title=\"`dictionary`\"><code>dictionary</code></a></td></tr>\n</tbody></table>\n<table><thead><tr><th>Typst value</th><th>Converted into JSON</th></tr></thead><tbody>\n<tr><td>types that can be converted from JSON</td><td>corresponding JSON value</td></tr>\n<tr><td><a href=\"/en-US-v0.14.1/reference/foundations/bytes/\" title=\"`bytes`\"><code>bytes</code></a></td><td>string via <a href=\"/en-US-v0.14.1/reference/foundations/repr/\" title=\"`repr`\"><code>repr</code></a></td></tr>\n<tr><td><a href=\"/en-US-v0.14.1/reference/foundations/symbol/\" title=\"`symbol`\"><code>symbol</code></a></td><td>string</td></tr>\n<tr><td><a href=\"/en-US-v0.14.1/reference/foundations/content/\" title=\"`content`\"><code>content</code></a></td><td>an object describing the content</td></tr>\n<tr><td>other types (<a href=\"/en-US-v0.14.1/reference/layout/length/\" title=\"`length`\"><code>length</code></a>, etc.)</td><td>string via <a href=\"/en-US-v0.14.1/reference/foundations/repr/\" title=\"`repr`\"><code>repr</code></a></td></tr>\n</tbody></table>\n<h3 id=\"notes\">Notes</h3>\n<ul>\n<li>\n<p>In most cases, JSON numbers will be converted to floats or integers\ndepending on whether they are whole numbers. However, be aware that\nintegers larger than 2<sup>63</sup>-1 or smaller than -2<sup>63</sup> will\nbe converted to floating-point numbers, which may result in an\napproximative value.</p>\n</li>\n<li>\n<p>Bytes are not encoded as JSON arrays for performance and readability\nreasons. Consider using <a href=\"/en-US-v0.14.1/reference/data-loading/cbor/#definitions-encode\" title=\"`cbor.encode`\"><code>cbor.encode</code></a> for binary data.</p>\n</li>\n<li>\n<p>The <code>repr</code> function is <a href=\"/en-US-v0.14.1/reference/foundations/repr/#debugging-only\">for debugging purposes only</a>,\nand its output is not guaranteed to be stable across Typst versions.</p>\n</li>\n</ul>"}],"self":false,"params":[{"name":"source","details":[{"kind":"html","content":"<p>A <a href=\"/en-US-v0.14.1/reference/syntax/#paths\">path</a> to a JSON file or raw JSON bytes.</p>"}],"types":["str","bytes"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":["any"],"scope":[{"path":["json"],"name":"decode","title":"Decode JSON","keywords":[],"oneliner":"Reads structured data from a JSON string/bytes.","element":false,"contextual":false,"deprecationMessage":"`json.decode` is deprecated, directly pass bytes to `json` instead","deprecationUntil":"0.15.0","details":[{"kind":"html","content":"<p>Reads structured data from a JSON string/bytes.</p>"}],"self":false,"params":[{"name":"data","details":[{"kind":"html","content":"<p>JSON data.</p>"}],"types":["str","bytes"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":["any"],"scope":[]},{"path":["json"],"name":"encode","title":"Encode JSON","keywords":[],"oneliner":"Encodes structured data into a JSON string.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Encodes structured data into a JSON string.</p>"}],"self":false,"params":[{"name":"value","details":[{"kind":"html","content":"<p>Value to be encoded.</p>"}],"types":["any"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"pretty","details":[{"kind":"html","content":"<p>Whether to pretty print the JSON with newlines and indentation.</p>"}],"types":["bool"],"strings":[],"default":"<code><span class=\"typ-key\">true</span></code>","positional":false,"named":true,"required":false,"variadic":false,"settable":false}],"returns":["str"],"scope":[]}]}}}