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 } }
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.
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.
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.
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.
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 }'
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 }}')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With