-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
httpie changing the json fields order in the output #427
Comments
Note that in the json formatter, sort_keys=True |
ah ok, thanks. With the following I could disable sorting the keys (unfortunately together with the indentation, but that is not that big of a problem)
|
You're welcome, although I feel that this does raise the question of wether the sorting is something that should be done in general or should it be as server intended |
would it be possible to introduce another value for |
could you make unordered as default? and an option like ➸ python3 -m json.tool -h
usage: python -m json.tool [-h] [--sort-keys] [infile] [outfile]
A simple command line interface for json module to validate and pretty-print
JSON objects.
positional arguments:
infile a JSON file to be validated or pretty-printed
outfile write the output of infile to outfile
optional arguments:
-h, --help show this help message and exit
--sort-keys sort the output of dictionaries alphabetically by key |
I just lost way more time than I'm comfortable admitting trying to track down a problem in my server-side JSON library because I couldn't work out why it was sending the data out in the wrong order. It didn't even occur to me that the client could be reordering stuff. Is it even worthwhile having the option to reorder JSON? 90% of the time it's going to obfuscate rather than improve server output.
|
fully agree with @carlfish |
@jkbrzt any thoughts on this? |
Tested this locally, and it looks like if you tell the formatter not to alphabetize, you get object keys in an arbitrary order instead - I guess it's backed by an unordered dictionary? In that case, alphabetization is probably better than random. |
yes, the python but there is workaround, please use >>> import json
>>> from collections import OrderedDict
>>> data = json.loads('{"foo":1, "bar": 2}', object_pairs_hook=OrderedDict)
>>> print json.dumps(data, indent=4)
{
"foo": 1,
"bar": 2
}
>>> |
Two shaved yaks later: Pull request -> #520 |
For what it's worth, I happen to be a user that likes seeing the keys in my JSON output sorted. My server doesn't define an order that it outputs the keys in, and if I'm looking to see if the key I expect was included in the JSON body or not, it's much easier when they're sorted. So I'd say not to just arbitrarily remove the sorting functionality, but rather make it configurable or toggleable via flag. |
ping author @jakubroztocil is this out of maintenance? |
Is there any traction on this issue? Like @carlfish, I have just spend an embarrassingly long amount of time trying to correct a bug in my server, only to find, that httpie was the issue. It seems very unintuitive that it would reorder/sort data from the server, without the user explicitly enabling it. |
There is a solution in PR #520, which unfortunately has not yet been merged. |
This would be really useful. Sorting is not that good when you don't want it. |
the whole Python2 deprecated; some maintainer can take a look of the pending PRs? from contributor graphs it looks like @jakubroztocil @msabramo still active? |
Is anybody working on this? Can we expect this to be solved? |
@opensas don't know about that but possible solution is to use
|
@opensas this feature will be included in the upcoming v2.2.0.
@nmtitov here it’s not actually |
thanks a lot for the tip, already noticed it in this thread. I mean, httpie returns this:
but http | jq -C strips the first part, returning only:
|
@opensas I don’t think it’s possible unless you write your custom bash wrapper |
@jakubroztocil this won't work because
|
I see. You could use |
I found the following workqround, instead of using httpie I found curlie, which seems to
So I can use it like this: $ curlie GET localhost:3000/tasks | jq -C
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 304
ETag: W/"130-ED1W4hQo1i7na7wy5Ewc7iKdoJc"
Date: Wed, 27 May 2020 06:28:26 GMT
Connection: keep-alive
[
{
"id": 2,
"title": "new task2",
"description": "description2",
"status": "OPEN"
},
{
"id": 3,
"title": "new task3",
"description": "description3",
"status": "OPEN"
}
] Where as with httpie I would the headers BTW, I created this convenient script: $ cat ~/bin/c
curlie "$@" | jq -C |
Just released v2.2.0 that addresses this issue. Learn about about the new |
Wondering how can I force httpie to not change the json fields order?
with httpie
I prefer the
id
field to be always the first. Any thoughts?The text was updated successfully, but these errors were encountered: