Skip to main content
function encode
def encode(
x: typing.Any,
/,
*,
indent: int = 
) -> str
Encode a value to a JSON string. Mirrors the Starlark stdlib’s json.encode, with an additional optional indent parameter: pass a non-negative integer to pretty-print the output with that many spaces of indentation per nesting level (newlines inserted between elements). Omitting indent produces the stdlib’s compact single-line form. function decode
def decode(
x: str,
/
) -> typing.Any
Decode a JSON string. Raises on parse failure. Mirrors the Starlark stdlib’s json.decode. Use try_decode instead at I/O boundaries where a malformed input would otherwise crash the caller. function try_decode
def try_decode(
x: str,
default: typing.Any = ,
/
) -> typing.Any
Decode a JSON string. Returns default (None by default) on parse failure instead of raising. Distinguishing “parse failed” from “valid null parse”: both produce None unless the caller passes a sentinel default.

Examples

json.try_decode('{"a": 1}')        # {"a": 1}
json.try_decode("not json")        # None
json.try_decode("not json", {})    # {}
json.try_decode("null")            # None  (valid parse)
json.try_decode("null", "MISS")    # None  (still valid; not the sentinel)