json.encode_indent
The json.encode_indent function converts a value to JSON and formats it with line breaks and indentation. It is
equivalent to calling json.encode and passing the result to
json.indent.
Usage
json.encode_indent(x, prefix = "", indent = "\t")
Arguments
x- Required. The value to encode. It must be passed positionally.
prefix- (Optional) Keyword-only. Text added at the start of every line after the first. Defaults to
"". indent- (Optional) Keyword-only. The text used for one level of indentation. Defaults to a tab character.
Returns
A string containing the formatted JSON. The value is encoded by the same rules as
json.encode, so dictionary keys appear in sorted order.
Examples
Print a readable document
data = {"name": "vpc", "count": 3, "tags": ["a", "b"], "note": None}
print(json.encode_indent(data, indent = " "))
{
"count": 3,
"name": "vpc",
"note": null,
"tags": [
"a",
"b"
]
}
Add a prefix to every line
print(json.encode_indent([1, {"a": 2}], prefix = "> ", indent = " "))
[
> 1,
> {
> "a": 2
> }
> ]
Return a readable step value
output = json.encode_indent({"service": "api", "replicas": 3}, indent = " ")
A string assigned to output is used as written, so the step value keeps the
indentation. Assigning the dictionary directly produces compact JSON.
Errors
- A value that cannot be encoded fails with the same errors as
json.encode, with the message prefixedjson.encode_indent:. - Passing
prefixorindentpositionally fails, because both are keyword-only.
Related
json.encodeproduces compact JSON.json.indentformats JSON text you already have.json.decodeparses JSON text.- Atmos Automation Language and the script step