How to use `jq` in a shell pipeline?
One use case I have found myself doing frequently as well is "How do I construct JSON data to supply into other shell commands, for example curl
?" The way I do this is by using the --null-input/-n
option:
Don’t read any input at all! Instead, the filter is run once using
null
as the input. This is useful when usingjq
as a simple calculator or to construct JSON data from scratch.
And an example passing it into curl
:
jq -n '{key: "value"}' | curl -d @- \ --url 'https://some.url.com' \ -H 'Content-Type: application/json' \ -H 'Accept: application/json'