Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use `jq` in a shell pipeline?

Tags:

shell

pipe

jq

I can't seem to get jq to behave "normally" in a shell pipeline. For example:

$ curl -s https://api.github.com/users/octocat/repos | jq | cat

results in jq simply printing out its help text*. The same thing happens if I try to redirect jq's output to a file:

$ curl -s https://api.github.com/users/octocat/repos | jq > /tmp/stuff.json

Is jq deliberately bailing out if it determines that it's not being run from a tty? How can I prevent this behavior so that I can use jq in a pipeline?

Edit: it looks like this is no longer an issue in recent versions of jq. I have jq-1.6 now and the examples above work as expected.


* (I realize this example contains a useless use of cat; it's for illustration purposes only)

like image 988
mgalgs Avatar asked Oct 14 '22 17:10

mgalgs


People also ask

What is jq in Shell?

The JQ command is used to transform JSON data into a more readable format and print it to the standard output on Linux. The JQ command is built around filters which are used to find and print only the required data from a JSON file.

What is pipe jq?

jq is a fantastic command-line JSON processor. It plays nice with UNIX pipes and offers extensive functionality for interrogating, manipulating and working with JSON file.

What does jq do in bash?

jq command is used not only for reading JSON data but also to display data by removing the particular key. The following command will print all key values of Students. json file by excluding batch key. map and del function are used in jq command to do the task.

What is jq tool?

jq is a Linux command line utility that is easily used to extract data from JSON documents. The source of a JSON document can be a response from a CLI command or the result of a REST API call, files retrieved from remote locations or read from local storage.


2 Answers

You need to supply a filter as an argument. To pass the JSON through unmodified other than the pretty printing jq provides by default, use the identity filter .:

curl -s https://api.github.com/users/octocat/repos | jq '.' | cat
like image 378
chepner Avatar answered Oct 16 '22 07:10

chepner


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 using jq 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'
like image 19
mkobit Avatar answered Oct 16 '22 06:10

mkobit