I have a large dictionary that has some large array data in it:
d = {\'something\': {\'else\': \'x\'}, \'longnumbers\': [1,2,3,4,54,6,67,7,7,8,8,8,6,4,3,3,5,6,7,
Ugh, should really be an option for specifying different indents for the two different JSON container types by now. An alternative approach if you want to stay compatible with the core Python JSON lib is to override the function (_make_iterencode()
currently) in that lib that is responsible for handling indent
.
Had a crack at reimplementation of _make_iterencode(). Only had to change a few lines to make the indent
option, optionally take a tuple (hash-indent, array-indent)
. But unfortunately have to replace an entire _make_iterencode()
which turns out to be pretty big and poorly decomposed. Anyway, following works for 3.4-3.6:
import sys
import json
dat = {"b": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "a": 1, "c": "x"}
indent = 2
print(json.dumps(dat, indent=indent))
if sys.version_info.major == 3 and 4 <= sys.version_info.minor <= 6:
import _make_iterencode
json.encoder._make_iterencode = _make_iterencode._make_iterencode
indent = (2, None)
print(json.dumps(dat, indent=indent))
Gives:
{
"c": "x",
"a": 1,
"b": [
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
]
}
{
"c": "x",
"a": 1,
"b": [1,2,3,4,5,6,7,8,9,10]
}