3 ms·
I used to do this, but now I use jq. The thing about the Python version is that Python doesn't have ordered dictionaries, so the output isn't in the same order
by 100k 11y ago
I used to do this, but now I use jq. The thing about the Python version is that Python doesn't have ordered dictionaries, so the output isn't in the same order as the raw JSON. jq doesn't have that issue which is nice.
- batbomb 11y agoPython does have ordered dictionaries, but the json decoder defaults to regular dictionaries. This is easily overridable with JSONDecoder.
- 100k 11y agoIs there a one-liner you can use from the shell that preserves order?
- hnov 11y agoStarting with 3.5[0] that's the standard behavior. One liner-wise, you could create an alias echo '{"foo":12, "bar": 11}' | python -c 'import json as j, collections as c, sys as s; j.dump(j.load(s.stdin, object_pairs_hook=c.OrderedDict), s.stdout, indent=4)' [0] https://github.com/python/cpython/blob/3.5/Lib/json/tool.py#L40-L41 https://github.com/python/cpython/blob/3.5/Lib/json/tool.py#...
- deleted 11y ago[deleted]