Skip to content

Commit

Permalink
httpie#427 pretty-print JSON objects in received key order
Browse files Browse the repository at this point in the history
  • Loading branch information
carlfish committed Sep 16, 2016
1 parent 497a917 commit 3f3e3f8
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions httpie/output/formatters/json.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import
import json
import collections

from httpie.plugins import FormatterPlugin

Expand All @@ -18,15 +19,15 @@ def format_body(self, body, mime):
if (self.kwargs['explicit_json'] or
any(token in mime for token in maybe_json)):
try:
obj = json.loads(body)
obj = json.loads(body, object_pairs_hook=collections.OrderedDict)
except ValueError:
pass # Invalid JSON, ignore.
else:
# Indent, sort keys by name, and avoid
# unicode escapes to improve readability.
body = json.dumps(
obj=obj,
sort_keys=True,
sort_keys=False,
ensure_ascii=False,
indent=DEFAULT_INDENT
)
Expand Down

0 comments on commit 3f3e3f8

Please sign in to comment.