I recently learned about an outstanding little tool, jq that allows you to pipe unformatted JSON output into it and then have that output reformatted and output to the screen in a very nicely formatted color-coded json layout. Eg:

echo '{"value": "New", "onclick": "CreateNewDoc()"}' | jq

I have found however, that I am unable to pipe the formatted results (from jq) into additional tools in my standard toolset, such as grep, sed, awk, etc. How can I make make this output pipe-able? Eg, this doesn't work:

'{"value": "New", "onclick": "CreateNewDoc()"}' | jq | grep value
  • 1
    This seems to be a usage question on a general-purpose command-line tool. It seems more appropriate for Super User or Linux & Unix? – Xiong Chiamiov Dec 20 '17 at 16:57
  • 2
    @XiongChiamiov - I posted here because DevOps seems significantly more likely to interact with JSON/REST and would benefit from 1) knowing about such a tool and 2) it's proper usage. I would categorize this as a DevOps command line tool, not general purpose. – James Shewey Dec 20 '17 at 18:11
  • My devs are far more familiar with these sorts of tools than I am, because they're building APIs and thus testing them constantly throughout the day. While that will of course vary from company to company, I still think that curl/httpie/jq/etc. are just general-purpose programmer-y tools, not DevOps-related tools at all. – Xiong Chiamiov Dec 26 '17 at 18:43

Ok tried to understansd your problem exactly, what happens is as follow:

$ echo '{"value": "New", "onclick": "CreateNewDoc()"}' | jq | grep value
jq - commandline JSON processor [version 1.5-1-a5b5cbe]
Usage: jq [options] <jq filter> [file...]
[rest of output is omitted for brevity]

The important thing to read here is the usage line (emphasis is mine):

Usage: jq [options] <jq filter> [file...]

The filter is not optional, if you don't give one, jq try to parse the rest of the command line as its filter and throw an error, a workaround for your case here is:

$ echo '{"value": "New", "onclick": "CreateNewDoc()"}' | jq '.' | grep value
  "value": "New",

which indeed grep "value" line as the filter pass is '.' which just pretty print the json content, but that's far from the best use of jq and if you wish to get only this line you'd better do:

$ echo '{"value": "New", "onclick": "CreateNewDoc()"}' | jq '.value'

If you wish the value without quotes then add -r option to jq like:

$ echo '{"value": "New", "onclick": "CreateNewDoc()"}' | jq -r '.value'

from jq --help:

-r output raw strings, not JSON texts;

that's a little introduction to jq, that's probably not enough to solve your problem at all but as you didn't specify your problem I can't help more than that.

  • I think you may have been overthinking my problem. This was exactly the solution. An empty filter resolves the issue. – James Shewey Dec 20 '17 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.