jq
jq copied to clipboard
Feature request: Option to suppress normal output
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
It's been three years - any chance of this?
@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.
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?
You can do:
jq -j '""'
Which will write a "raw" empty string with no trailing newline. ie. nothing
Can also use empty to output nothing
$ echo 123 | jq '., "abc" | empty'
$
@wader That wouldn't work with the intention of this issue; --exit-status.
@itchyny ah sorry 👍 note to self: read whole thread first
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.
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).
I have also just found myself trying jq -eq and was surprised to see it doesn't exist; would love to see this implemented
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
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?
I think a simple -q|--quiet flag is extremely ubiquitous and it is rather surprising to learn jq doesn't support it.
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