Tools, FAQ, Tutorials:
Generating JSON Strings in Pretty Format
How to generate JSON strings in pretty format using json.dumps()?
✍: FYIcenter.com
You can control the JSON output string format with
"indent", "separators" and "sort_keys" arguments of the json.dumps() function:
Here is a Python example that generates a pretty formatted JSON string with property keys sorted:
>>> import json
>>> l = ['foo', {'bar': ('baz', None, 1.0, {"c": 0, "b": 0, "a": 0})}]
>>> j = json.dumps(l, indent=3, sort_keys=True)
>>> print(j)
[
"foo",
{
"bar": [
"baz",
null,
1.0,
{
"a": 0,
"b": 0,
"c": 0
}
]
}
]
You can set indent=None and separators=(',', ':') to get the most compact JSON string:
>>> import json
>>> l = ['foo', {'bar': ('baz', None, 1.0, {"c": 0, "b": 0, "a": 0})}]
>>> j = json.dumps(l, indent=None, separators=(',', ':'), sort_keys=True)
>>> print(j)
["foo",{"bar":["baz",null,1.0,{"a":0,"b":0,"c":0}]}]
⇐ json.dumps() - Dumping Object into JSON
2018-10-08, ∼1870🔥, 0💬
Popular Posts:
How to login to Azure API Management Publisher Dashboard? If you have given access permission to an ...
How to run CMD Commands in Dockerfile to change Windows Docker images? When building a new Windows i...
How to add an API to an API product for internal testing on the Publisher Portal of an Azure API Man...
What is EPUB 3.0 Metadata "dc:publisher" and "dc:rights" elements? EPUB 3.0 Metadata "dc:publisher" ...
How to add request query string Parameters to my Azure API operation 2017 version to make it more us...