json.indent
The json.indent function reformats a JSON string with line breaks and indentation. Use it to make JSON text
from a command or file readable. To encode a value and format it in one call, use
json.encode_indent.
Usage
json.indent(str, prefix = "", indent = "\t")
Arguments
str- Required. A valid JSON string. 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 with the same JSON content, spread across lines. Key order is preserved as written in the input.
Examples
Indent with two spaces
print(json.indent('{"a":1,"b":[1,2]}', indent = " "))
{
"a": 1,
"b": [
1,
2
]
}
Use the default tab indentation
print(json.indent('{"a":1,"b":[1,2]}'))
{
"a": 1,
"b": [
1,
2
]
}
Format command output
result = atmos.run(
["describe", "component", "vpc", "--stack=dev", "--format=json"],
output = "capture",
)
output = json.indent(result.stdout, indent = " ")
Errors
- Input that is not valid JSON fails with a message such as
json.indent: invalid character 'b' looking for beginning of object key string. - Passing
prefixorindentpositionally fails, because both are keyword-only.
Related
json.encode_indentencodes a value and indents it in one call.json.encodeandjson.decode- Atmos Automation Language and the script step