Encodes given object to a JSON string.
Identical to json.encode
, except that the output is a string rather than
bytes.
Usually, json.encode
should be preferred to this function. Use this function
only if you need to process the output as text. If you intend to send the
resulting text payload over the network, always use UTF-8 charset, per
https://tools.ietf.org/html/rfc8259#section-8.1.
If indent
is provided, it must be either a boolean, or a dictionary
with two optional string attributes: prefix
(defaulting to an empty string),
and indent
(defaulting to four spaces).
Setting indent
to true
is equivalent to setting it to an empty dictionary.
Setting indent
to false
is equivalent to not setting it.
When the indentation is enabled, each JSON element begins on a new line
beginning with prefix
and followed by zero or more copies of indent
according to the structural nesting.
Arguments
Arguments | |
---|---|
data |
The input to be encoded. |
indent |
Optional indentation options. |
Returns
The encoded JSON string.
Raised exceptions
Exceptions | |
---|---|
ValueError |
If the input contains types that cannot be JSON-encoded. Examples of such types include callable and bytes. |
Examples
# Log the values of a map - init: assign: - my_map: {"key1": "hello"} - print_callback_result: call: sys.log args: severity: "INFO" text: ${"Received " + json.encode_to_string(my_map)}