Stay organized with collections
Save and categorize content based on your preferences.
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.logargs:severity:"INFO"text:${"Received " + json.encode_to_string(my_map)}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# Function: json.encode_to_string\n\nEncodes given object to a JSON string.\n\nIdentical to `json.encode`, except that the output is a string rather than\nbytes.\n\nUsually, `json.encode` should be preferred to this function. Use this function\nonly if you need to process the output as text. If you intend to send the\nresulting text payload over the network, always use UTF-8 charset, per\n\u003chttps://tools.ietf.org/html/rfc8259#section-8.1\u003e.\n\nIf `indent` is provided, it must be either a boolean, or a dictionary\nwith two optional string attributes: `prefix` (defaulting to an empty string),\nand `indent` (defaulting to four spaces).\nSetting `indent` to `true` is equivalent to setting it to an empty dictionary.\nSetting `indent` to `false` is equivalent to not setting it.\n\nWhen the indentation is enabled, each JSON element begins on a new line\nbeginning with `prefix` and followed by zero or more copies of `indent`\naccording to the structural nesting.\n\nArguments\n---------\n\nReturns\n-------\n\nThe encoded JSON string.\n\nRaised exceptions\n-----------------\n\nExamples\n--------\n\n```yaml\n# Log the values of a map\n- init:\n assign:\n - my_map: {\"key1\": \"hello\"}\n- print_callback_result:\n call: sys.log\n args:\n severity: \"INFO\"\n text: ${\"Received \" + json.encode_to_string(my_map)}\n```"]]