jq icon indicating copy to clipboard operation
jq copied to clipboard

Feature request: Option to suppress normal output

Open fearphage opened this issue 7 years ago • 14 comments

I quite often want to confirm that some key is in a dictionary or something similar and I rely on jq --exit-status to relay that information. I rarely use the output from the command at that point. It would be convenient to be able to suppress normal output entirely.

Use case

is-key-valid() {
  echo "$1" | jq --exit-status '."'$2'"' > /dev/null
}

is-key-valid '{"foo": "bar"}' catpants

With this option, there'd be no need to pipe stdout to /dev/null. This is very similar to the grep option:

✗ grep --help | grep quiet
  -q, --quiet, --silent     suppress all normal output

fearphage avatar Sep 03 '16 19:09 fearphage

It's been three years - any chance of this?

G-Rath avatar Sep 28 '19 09:09 G-Rath

@G-Rath - Apparently not anytime soon.

So, besides redirecting /dev/null (or NUL:), you may also wish to consider using halt_error/1, e.g.

jq -n '{a:1} | if has("a") then null|halt_error(1) else null|halt_error(2) end'
echo rc=$?

The key here is that null|halt_error(N) will emit nothing.

pkoppstein avatar Sep 28 '19 18:09 pkoppstein

Yes, but in systemd's ExecCondition= redirecting stdout to /dev/null is not available!

I would disable normal (stdout) output if --exit-status is used. What is the reason for someone to check the output if they want to check the code?

socketpair avatar Dec 20 '22 11:12 socketpair

You can do: jq -j '""' Which will write a "raw" empty string with no trailing newline. ie. nothing

themadsens avatar Jun 22 '23 08:06 themadsens

Can also use empty to output nothing

$ echo 123 | jq '., "abc" | empty'
$

wader avatar Jun 22 '23 08:06 wader

@wader That wouldn't work with the intention of this issue; --exit-status.

itchyny avatar Jun 22 '23 08:06 itchyny

@itchyny ah sorry 👍 note to self: read whole thread first

wader avatar Jun 22 '23 09:06 wader

I doubt it's a good idea to change the behavior of --exit-status. However, I agree that the request makes sense.

Maybe a solution could be to add --exit-status-only variant which will do as the OP suggested: work as the original --exit-status but suppress all output, including stderr. We could consider deprecating the old option.

leonid-s-usov avatar Jun 22 '23 15:06 leonid-s-usov

I agree with adding -q, --quiet options, since it covers JSON file validation use case without --exit-status option (on validating a JSON file; the content could be just false).

itchyny avatar Jun 25 '23 13:06 itchyny

I have also just found myself trying jq -eq and was surprised to see it doesn't exist; would love to see this implemented

emilyyyylime avatar Nov 04 '23 15:11 emilyyyylime

I also came here hoping to see it was implemented

Use-case: a ExecCondition on a systemd service file. Sure I can add redir to /dev/null burt would havbe to do that in an extra shell

ExecCondition=/bin/sh -c "jq -e '...' .../file.json > /dev/null"

which is ugly. It would be much cleaner to do with a -q

ExecCondition=jq -qe '...' .../file.json

nhed avatar Dec 14 '23 16:12 nhed

We've been resistant to adding features that the shells and shell utilities can handle. See also edit-in-place for example.

I'm less opposed to this one than to edit-in-place. Maybe the other admins want to accept this?

nicowilliams avatar Jan 16 '24 22:01 nicowilliams

I think a simple -q|--quiet flag is extremely ubiquitous and it is rather surprising to learn jq doesn't support it.

emilyyyylime avatar Feb 08 '24 10:02 emilyyyylime

I tried to implement this some months ago and if i remember correctly it got quite messy to support it doing input validation, streamed input and jq program errors with the current code. Maybe part of doing this is to reorganise process in main.c a bit to make it less of a mess? (could also be that don't understand the code well!) also part of this is probably to add some good tests for it and combined with other options to not cause regressions

wader avatar Feb 08 '24 11:02 wader