-
Notifications
You must be signed in to change notification settings - Fork 102
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
Add support for nested JSON #3
Comments
Some experiments I've done with nom https://gist.github.com/ducaale/5d3a3df08a39d128eedab362aac34850. The plan is to support syntax like this: $ xh httpbin.org/post foo.bar=5 # {foo: {bar: 5}}
$ xh httpbin.org/post foo[path.with.dots]=7 # {foo: {'path.with.dots': 7}}
$ xh httpbin.org/post foo[]=5 foo[]=6 # {foo: [5, 6]}
$ xh httpbin.org/post foo\.bar=5 # {'foo.bar': 5} Things to consider:
For the third question, https://github.com/jdp/jarg may have the answer: $ jarg 'foo[]=5' 'foo[bar]=6'
{"foo": {"0": 5, "bar": 6}}
$ jarg 'foo[bar]=5' 'foo[]=6'
{"foo": {"bar": 5, "": 6}} |
I don't see why not.
Are there use cases for that? It seems a bit niche, and treating anything between brackets as a string would be simpler.
I think Question 4: How are these marked? There's no fully backward compatible way to add a new separator. (It would be fully backward compatible to change the meaning of an existing separator using a flag, but that's tedious and doesn't let you mix.) HTTPie already has Ideally a separator would be made up of those existing special characters. But Another solution would be to leave the separator alone, and add a marker to the key. Maybe a leading dot? Then you'd have e.g. That would also make the value more flexible, because then you can combine it with the existing separators. It would still change the meaning of some commands that already work right now. I don't know how common keys with a leading dot are. |
One use case I can think of is accessing the same index in array $ xh httpbin.org/post [0].foo=7 [0].bar=5 [1].baz=3 # [{foo: 7, bar: 5}, {baz: 3}]
I think empty strings is a valid use case although I am starting to think that optimizing for every edge case might not be ideal.
I like this approach. However, I wonder if keys with dots are used that much. And if so, they could always be escaped, right? |
Ah, of course. That makes sense.
You could also express those with
It's perhaps not that common, but I expect it does happen. Jakub brought it up as a constraint in the HTTPie issue, particularly for scripts (that may run without supervision). Escaping is a bit problematic because it's not compatible with current versions. It would be nice to have a design that could also be adopted by HTTPie, I would be willing to port it. When we have a prototype (or a detailed design) we should post it in the issue for feedback. |
Some ideas to tackle the compatibility issue with the nested JSON syntax:
|
I think that would be very confusing.
That wouldn't work, because old versions would reject the flag. And if you know your version is new enough you can just escape the dots.
A flag would be a bit unergonomic but besides that it would work. It would have perfect backward compatibility because trying to use it on an old version would simply give an error. A config option would be confusing because it reduces copy/pastability. HTTPie also recommends against configuring default flags that change behavior significantly. I just thought of another syntax with perfect backward compatibility: a leading colon. HTTPie and xh both currently parse The downside is that it's unintuitive. A leading dot at least looks like jq. |
What if we use a single dot to activate the feature e.g |
Hm, that one's interesting too. But I'm not sure there's a reason to prefer it over a one-character flag. |
Httpie's approach to nested JSON syntax httpie/cli#1169 |
HTTPie's nested JSON has been revised in a subsequent PR:
|
Not super relevant, but a proposed JSON syntax for cURL has some form of nesting. See https://github.com/curl/curl/wiki/JSON |
Excited for this capability in xh. Is using --raw the recommended approach until then? |
There are two other approaches besides
For more information, see https://httpie.io/docs/cli/raw-request-body. |
Resolved in #217 |
See httpie/cli#78
The text was updated successfully, but these errors were encountered: