Reputation: 5545
I am using python's json
to produce a json file and cannot manage to format it the way I want.
What I have is a dictionary with some keys, and each key has a list of numbers attached to it:
out = {"a": [1,2,3], "b": [4,5,6]}
What I want to do is produce a JSON string where each list is in its own line, like so:
{
"a": [1,2,3],
"b": [4,5,6]
}
However, I can only get
>>> json.dumps(out)
'{"a": [1, 2, 3], "b": [4, 5, 6]}'
which has no new lines, or
>>> print json.dumps(out, indent=2)
{
"a": [
1,
2,
3
],
"b": [
4,
5,
6
]
}
which has waaay to many. Is there a simply way to produce the string I want? I can do it manually, of course, but I am wondering if it is possible with json
alone...
Upvotes: 1
Views: 42
Reputation: 1124170
You can't do that with the json
module, no. It was never the goal for the module to allow this much control over the output.
The indent
option is only meant to aid debugging. JSON parsers don't care about how much whitespace is used in-between elements.
Upvotes: 1