Skip to content
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

Syntax for nested JSON #78

Closed
sym3tri opened this issue Jul 26, 2012 · 23 comments · Fixed by #1169 or #1224
Closed

Syntax for nested JSON #78

sym3tri opened this issue Jul 26, 2012 · 23 comments · Fixed by #1169 or #1224
Labels
enhancement New feature or enhancement needs product design We like the idea, but we want to explore the problem deeper, and consider the solution holistically planned Solution is being worked on
Milestone

Comments

@sym3tri
Copy link

sym3tri commented Jul 26, 2012

The simple examples work, but what about nested data?
In curl I do something like this:

-d
'"credentials": {
"username": "me",
"key": "my-key"}
}'

How can I do this with httpie?

@jkbrzt
Copy link
Member

jkbrzt commented Jul 26, 2012

For deeply nested JSON it's easier to use stdin. For example:

 echo '{"foo": {"bar": "baz"}}' | http url

 # Or from a file:
 http url < file.json

More examples are listed in the README.

@jkbrzt jkbrzt closed this as completed Jul 26, 2012
@sandstrom
Copy link

httpie is a dream! nesting would make it even more awesome though.

It would be nice with rails-like nesting, e.g. path/to/endpoint credentials[username]=me

@jkbrzt jkbrzt reopened this May 29, 2015
@jkbrzt jkbrzt changed the title How do you input nested json? Syntax for nested JSON May 29, 2015
@mfussenegger
Copy link

Some sort of easier syntax for nested data would really be a nice thing to have.
There is also another issue with some syntax suggestions: #346

@jkbrzt
Copy link
Member

jkbrzt commented May 29, 2015

So nesting could be expressed either with brackets or using dot notation.

@akamalov
Copy link

Greetings!!! Small question regarding nested json and httpie. So I have a json file:

{
      "apps": [
         { "id": "/product/web/tutum-hello-world-2",
                   "container": {
               "type": "DOCKER",
                   "docker": {
                     "image": "tutum/hello-world",
                         "network": "BRIDGE"
                          }
                        },
                        "id": "/product/web/tutum-hello-world-2",
                        "instances": 1,
                        "cpus": 0.25,
                        "mem": 256,
                        "uris": [],
                        "constraints": [["hostname", "LIKE", "server1.localnet.net"]]

                }
        ]
}

I want to increase the value of "instances": to 2 and use "constraints": [["hostname", "LIKE", "server1.localnet.net"]]. I tried different permutations, but I am getting No JSON object could be decoded over and over again :(

Using just instances: is pretty easy:

root@node108:~# http -v PUT http://10.10.10.219:8080/v2/apps/product/web/tutum-hello-world-1 instances:=1
PUT /v2/apps/product/web/tutum-hello-world-1 HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate, compress
Content-Length: 16
Content-Type: application/json; charset=utf-8
Host: 10.9.158.219:8080
User-Agent: HTTPie/0.8.0

{
    "instances": 1
}

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, must-revalidate
Content-Length: 92
Content-Type: application/json
Expires: 0
Pragma: no-cache
Server: Jetty(8.y.z-SNAPSHOT)

{
    "deploymentId": "6eeda6ac-2ec1-4554-95f3-a0dc19333c56", 
    "version": "2015-06-10T11:33:53.165Z"
}

...however using it TOGETHER with constraints directive + nested values has proven to be difficult (for me). Any help, please ???

Thanks!!!!

@akamalov
Copy link

Answering my own question:

http -v PUT http://10.10.10.219:8080/v2/apps/product/web/tutum-hello-world-1 instances:=2 constraints:='[["hostname", "LIKE", server1.localnet.nett"]]'

PUT /v2/apps/product/web/tutum-hello-world-1 HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate, compress
Content-Length: 85
Content-Type: application/json; charset=utf-8
Host: 10.9.158.219:8080
User-Agent: HTTPie/0.8.0

{
    "constraints": [
        [
            "hostname", 
            "LIKE", 
            "server1.localnet.net"
        ]
    ], 
    "instances": 2
}

HTTP/1.1 200 OK
Cache-Control: no-cache, no-store, must-revalidate
Content-Length: 92
Content-Type: application/json
Expires: 0
Pragma: no-cache
Server: Jetty(8.y.z-SNAPSHOT)

{
    "deploymentId": "bde62f56-89f6-406a-9c3a-d0309ea3dde6", 
    "version": "2015-06-10T12:28:06.056Z"
}

Thanks!!

Alex

@jkbrzt jkbrzt added the enhancement New feature or enhancement label Jun 26, 2015
@jkbrzt
Copy link
Member

jkbrzt commented Mar 7, 2016

Syntax: https://www.w3.org/TR/html-json-forms/

(https://github.com/jdp/jarg)

@lastcoolnameleft
Copy link

@jkbrzt For clarification are you referencing jarg for your own notes/feature development? I tried the following which didn't work:

http post localhost:3000/api foo[bar]=baz

Is received as:

body= { 'foo[bar]': 'baz' }

However, piping through jarg does seem to work:

jarg foo[bar]=baz | http post localhost:3000/api

Is received as:

body= { foo: { bar: 'baz' } }

@jkbrzt
Copy link
Member

jkbrzt commented Apr 12, 2016

@lastcoolnameleft yes, I was just bookmarking that spec. Sorry for the confusion.

@Maatary
Copy link

Maatary commented May 13, 2016

Hi, I am not sure to understand, how do you provided nested json object with Httpie

@akvadrako
Copy link

I would prefer javascript compatible syntax, so http post parent.child=value and the more complex syntax for embedded dots: [parent.with.dots]=value. The common use case should be easiest and json properties with dots are ugly to use in javascript.

@rubemz
Copy link

rubemz commented Dec 18, 2017

any updates on this issue?
I'd also prefer what @akvadrako suggested

@odusseys
Copy link

agree that this would be very welcome.

@nikolay
Copy link

nikolay commented Jun 27, 2018

@jakubroztocil This issue has been open for 6 years! And it is such a rudimentary feature! Of course, we can all do echo, but we can use curl as well, right? The point is that httpie does all the basic REST operations. Of course, we can use it along with jarg or jo, but it's so easy to implement this within httpie, I think!

@merwok
Copy link

merwok commented Jun 27, 2018

I don’t think it’s that simple, or someone would have sent a pull request!

We should appreciate projects written on free time and given for free.

@nikolay
Copy link

nikolay commented Jun 27, 2018

@merwork We appreciate it. But how many REST services you know to have only a flat JSON?

@merwok
Copy link

merwok commented Jun 27, 2018

Many that I write :)

Thanks for the pointers to jarg and jo! Goes well in my json toolbox with jq and gron.

@segevfiner
Copy link

If you don't like piping JSON from jo or jarg before the http command. you can also use shell process substitution so the JSON is in a similar location when using the builtin syntax of HTTPie:

https postman-echo.com/post @<(jo hello=world)

It would still be much better if HTTPie supported a syntax like those tools of course.

@Almad Almad added needs product design We like the idea, but we want to explore the problem deeper, and consider the solution holistically planned Solution is being worked on labels Feb 3, 2021
@jkbrzt
Copy link
Member

jkbrzt commented Feb 10, 2021

An important consideration here is backwards compatibility. If we simply introduce a syntax relying on dots, than a script containing the following HTTPie command:

$ http httpbin.org/post  foo.bar=baz

Which currently produces this JSON:

{
    "foo.bar": "baz"
}

Would unexpectly result in:

{
    "foo": {
        "bar": "baz"
    }
}

So the command would have to be updated to escape the newly-meanignful . character:

$ http httpbin.org/post  'foo\.bar=baz'

@merwok
Copy link

merwok commented Feb 10, 2021

+1 for the compat issue, but the proposed solution should be more complicated:

$ http httpbin.org/post "foo\.bar=baz"

otherwise the shell escape \. is interpreted by the shell as . and the backslash is not passed to the command called.

@jkbrzt
Copy link
Member

jkbrzt commented Feb 10, 2021

@merwok Good point. I’ve updated the example to take shell escaping into account. General request item escaping rules apply.

@BoboTiG
Copy link
Contributor

BoboTiG commented Sep 29, 2021

Note that since HTTPie 2.5.0, you can use --raw in the same way you do curl -d:

$ http pie.dev/post --raw '{"foo": {"bar": "baz"}}'
{
    "foo": {
        "bar": "baz"
    }
}

@isidentical
Copy link
Contributor

🎉 This feature is now publicly available in HTTPie for Terminal 3.0:

👉🏻 https://httpie.io/docs/cli/nested-json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or enhancement needs product design We like the idea, but we want to explore the problem deeper, and consider the solution holistically planned Solution is being worked on
Projects
None yet
17 participants