{"route":"/en-US-v0.13.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":"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,"deprecation":null,"details":"<p>Reads structured data from a JSON file.</p>\n<p>The file must contain a valid JSON value, such as object or array. JSON\nobjects will be converted into Typst dictionaries, and JSON arrays will be\nconverted into Typst arrays. Strings and booleans will be converted into the\nTypst equivalents, <code>null</code> will be converted into <code><span class=\"typ-key\">none</span></code>, and numbers will\nbe converted to floats or integers depending on whether they are whole\nnumbers.</p>\n<p>Be aware that integers larger than 2<sup>63</sup>-1 will be converted to\nfloating point numbers, which may result in an approximative value.</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.13.1/assets/f531864e1bdd9f39c36d5451a39f87b2.png\" alt=\"Preview\"></div></div>","example":null,"self":false,"params":[{"name":"source","details":"<p>A <a href=\"/en-US-v0.13.1/reference/syntax/#paths\">path</a> to a JSON file or raw JSON bytes.</p>","example":null,"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,"deprecation":"`json.decode` is deprecated, directly pass bytes to `json` instead","details":"<p>Reads structured data from a JSON string/bytes.</p>","example":null,"self":false,"params":[{"name":"data","details":"<p>JSON data.</p>","example":null,"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,"deprecation":null,"details":"<p>Encodes structured data into a JSON string.</p>","example":null,"self":false,"params":[{"name":"value","details":"<p>Value to be encoded.</p>","example":null,"types":["any"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"pretty","details":"<p>Whether to pretty print the JSON with newlines and indentation.</p>","example":null,"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":[]}]}}}