> For the complete documentation index, see [llms.txt](https://developers.neowit.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.neowit.io/starlark/modules/json-module.md).

# json module

The original source for this documentation can be found here: <https://github.com/google/starlark-go/blob/master/lib/json/json.go>

Module json is a Starlark module of JSON-related functions.

## def encode(x):

The encode function accepts one required positional argument, which it converts to JSON by cases:

* A Starlark value that implements Go's standard json.Marshal // interface defines its own JSON encoding.
* None, True, and False are converted to null, true, and false, respectively.
* Starlark int values, no matter how large, are encoded as decimal integers. Some decoders may not be able to decode very large integers.
* Starlark float values are encoded using decimal point notation, even if the value is an integer. It is an error to encode a non-finite floating-point value.
* Starlark strings are encoded as JSON strings, using UTF-16 escapes.
* A Starlark IterableMapping (e.g. dict) is encoded as a JSON object. It is an error if any key is not a string.
* Any other Starlark Iterable (e.g. list, tuple) is encoded as a JSON array.
* A Starlark HasAttrs (e.g. struct) is encoded as a JSON object.

If an application-defined type matches more than one the cases describe above, (e.g. it implements both Iterable and HasFields), the first case takes precedence. Encoding any other value yields an error.

## def decode(x\[, default]):

The decode function has one required positional parameter, a JSON string. It returns the Starlark value that the string denotes.

* Numbers are parsed as int or float, depending on whether they contain a decimal point.
* JSON objects are parsed as new unfrozen Starlark dicts.
* JSON arrays are parsed as new unfrozen Starlark lists.

If x is not a valid JSON string, the behavior depends on the "default" parameter: if present, decode returns its value; otherwise, decode fails.

## def indent(str, \*, prefix="", indent="\t"):

The indent function pretty-prints a valid JSON encoding, and returns a string containing the indented form. It 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.
