Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending nested JSON object using HTTPie

Tags:

json

httpie

I am trying to use HTTPie to parse to send some nested JSON object, but I can not find how. It is pretty clear how to send a JSON object but not a nested one such as

{ "user": { "name": "john" "age": 10 } }

like image 527
MaatDeamon Avatar asked May 13 '16 16:05

MaatDeamon


People also ask

How do I access a nested object in JSON?

Accessing nested json objects is just like accessing nested arrays. Nested objects are the objects that are inside an another object. In the following example 'vehicles' is a object which is inside a main object called 'person'. Using dot notation the nested objects' property(car) is accessed.

Can you nest objects in JSON?

Objects can be nested inside other objects. Each nested object must have a unique access path. The same field name can occur in nested objects in the same document.

How can we parse a nested JSON object in Java?

We can parse a nested JSON object using the getString(index) method of JSONArray. This is a convenience method for the getJSONString(index). getString() method and it returns a string value at the specified position.

Can you have nested arrays in JSON?

JSON can store nested objects in JSON format in addition to nested arrays. These objects and arrays will be passed as values assigned to keys, and typically will be comprised of key-value pairs as well.


2 Answers

Update for HTTPie 3.0 released in January 2022:

There’s now built-in support for nested JSON using the HTTPie language:

$ http pie.dev/post \   tool[name]=HTTPie \   tool[about][homepage]=httpie.io \   tool[about][mission]='Make APIs simple and intuitive' \   tool[platforms][]=terminal \   tool[platforms][]=desktop \   tool[platforms][]=web \   tool[platforms][]=mobile  
{     "tool": {         "name": "HTTPie",         "about": {             "mission": "Make APIs simple and intuitive"             "homepage": "httpie.io",         },         "platforms": [             "terminal",             "desktop",             "web",             "mobile",         ]     } } 

You can learn more about nested JSON in the docs: https://httpie.io/docs/cli/nested-json


Old answer for HTTPie older than 3.0:

You can pass the whole JSON via stdin:

$ echo '{ "user": { "name": "john", "age": 10 } }' | http httpbin.org/post 

Or specify the raw JSON as value with :=:

$ http httpbin.org/post user:='{"name": "john", "age": 10 }' 
like image 83
Jakub Roztocil Avatar answered Oct 05 '22 20:10

Jakub Roztocil


I like this way:

$ http PUT localhost:8080/user <<<'{ "user": { "name": "john", "age": 10 }}' 

It is preferrable because it has the same prefix as the related commands, and so it is convenient to find the commands with Ctrl+R in bash:

$ http localhost:8080/user/all $ http GET localhost:8080/user/all # the same as the previous $ http DELETE localhost:8080/user/234 

If you have fishshell, which doesn't have Here Strings, I can propose the following workaround:

~> function tmp; set f (mktemp); echo $argv > "$f"; echo $f; end ~> http POST localhost:8080/user < (tmp '{ "user": { "name": "john", "age": 10 }}') 
like image 41
Nick Linker Avatar answered Oct 05 '22 18:10

Nick Linker