docs/versions/6.2.0/rules/lib/json.mdx
Module json is a Starlark module of JSON-related functions.
<h2>Members</h2> <ul> <li> <a href="#decode">decode</a> </li> <li> <a href="#encode">encode</a> </li> <li> <a href="#encode_indent">encode_indent</a> </li> <li> <a href="#indent">indent</a> </li> </ul> <h2 id="decode">decode</h2>
<p><pre class="rule-signature">unknown json.decode(x)</pre></p>
The decode function accepts one positional parameter, a JSON string.
It returns the Starlark value that the string denotes.
<ul><li>'null', 'true', and 'false' are parsed as None, True, and False. <li>Numbers are parsed as int, or as a float if they contain a decimal point or an exponent. Although JSON has no syntax for non-finite values, very large values may be decoded as infinity. <li>a JSON object is parsed as a new unfrozen Starlark dict. If the same key string occurs more than once in the object, the last value for the key is kept. <li>a JSON array is parsed as new unfrozen Starlark list. </ul> Decoding fails if x is not a valid JSON encoding. <!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="decode.x">
<code>x</code>
</td>
<td>
required
</td>
</tr>
</tbody>
</table>
<h2 id="encode">encode</h2>
<p><pre class="rule-signature"><a class="anchor" href="string.html">string</a> json.encode(x)</pre></p>
<p>The encode function accepts one required positional argument, which it converts to JSON by cases:
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="encode.x">
<code>x</code>
</td>
<td>
required
</td>
</tr>
</tbody>
</table>
<h2 id="encode_indent">encode_indent</h2>
<p><pre class="rule-signature"><a class="anchor" href="string.html">string</a> json.encode_indent(x, *, prefix='', indent='\t')</pre></p>
The encode_indent function is equivalent to <code>json.indent(json.encode(x), ...)</code>. See <code>indent</code> for description of formatting parameters.
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="encode_indent.x">
<code>x</code>
</td>
<td>
required
</td>
</tr>
<tr>
<td id="encode_indent.prefix">
<code>prefix</code>
</td>
<td>
default = ''
</td>
</tr>
<tr>
<td id="encode_indent.indent">
<code>indent</code>
</td>
<td>
default = '\t'
</td>
</tr>
</tbody>
</table>
<h2 id="indent">indent</h2>
<p><pre class="rule-signature"><a class="anchor" href="string.html">string</a> json.indent(s, *, prefix='', indent='\t')</pre></p>
The indent function returns the indented form of a valid JSON-encoded string.
Each array element or object field appears on a new line, beginning with the prefix string followed by one or more copies of the indent string, according to its nesting depth. The function accepts one required positional parameter, the JSON string, and two optional keyword-only string parameters, prefix and indent, that specify a prefix of each new line, and the unit of indentation. If the input is not valid, the function may fail or return invalid output.
<!-- hide-from-toc is a class used by DevSite for the public Bazel site
(https://developers.google.com/devsite/reference/styles/headings#hide_headings_from_the_toc) -->
<h3 class="hide-from-toc">Parameters</h3>
<table class="table table-bordered table-condensed table-params">
<colgroup>
<col class="col-param">
<col class="param-description">
</colgroup>
<thead>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td id="indent.s">
<code>s</code>
</td>
<td>
required
</td>
</tr>
<tr>
<td id="indent.prefix">
<code>prefix</code>
</td>
<td>
default = ''
</td>
</tr>
<tr>
<td id="indent.indent">
<code>indent</code>
</td>
<td>
default = '\t'
</td>
</tr>
</tbody>
</table>