Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jq -e always returns 0 for empty input #1142

Closed
rfletcher opened this issue May 5, 2016 · 5 comments · Fixed by #1697
Closed

jq -e always returns 0 for empty input #1142

rfletcher opened this issue May 5, 2016 · 5 comments · Fixed by #1697

Comments

@rfletcher
Copy link

rfletcher commented May 5, 2016

jq -e always exits with status 0 when passed empty input. For example:

$ echo | jq --exit-status 'false'; echo $?
0

Consider a use case like this one:

call-some-json-api |
jq --exit-status '.field == "some-value"' &&
take-action

If the API call fails and output is only written to stderr, take-action will still run as though the call succeeded, and "some-value" had been set.

This may be related to issue #667, but that one has been fixed on master, and this bug is still present.

@ryo1kato
Copy link
Contributor

ryo1kato commented May 6, 2016

A minor (one line) change to my fix proposal (PR #1140) for related #1139 issue should fix this.
Simply change the default ret value to '4' would break --exit-status semantics,
(it would always return 4 in such a case, while it should return 0 if -e is not set)

@TylerRick
Copy link

TylerRick commented Mar 13, 2018

Exactly, @rfletcher! With the current behavior of jq -e, the calling script is forced to either call the input command twice or saving the result in some intermediate buffer to avoid calling it a second time.

I ended up with a workaround like this in my script:

  # Exit with failure code (1) if we have no input
  # Otherwise jq will happily exit with 0 (https://github.com/stedolan/jq/issues/1142)
  some_input_script &>/dev/null || exit 1
  some_input_script | jq --exit-status ".$key == true" >/dev/null

There appears to be no way to handle or work around this with just a single jq filter/invocation — but there should be! (Even if we had to add another flag, like --empty-input-as-failure (or --empty-input-as-null, see #1628), that would be better than nothing.)

By the way, this issue came up again in #1497.

@scottleibrand
Copy link

For anyone who (like me) comes across this trying to check whether a file is valid json, you can use one of these workarounds until the underlying jq issue is fixed:

To just check that filename.json is valid json:
[ -s filename.json ] && jq -e . filename.json && echo filename.json is valid || echo invalid json

To also check for the presence of a .foo key:
[ -s filename.json ] && jq -e .foo filename.json && echo filename.json contains .foo || echo nope

ryo1kato added a commit to ryo1kato/jq that referenced this issue Jul 28, 2018
* Set default error code to -4 in main(), Fixes jqlang#1142
* fix --exit-code with more than one object in input, Fixes jqlang#1139
    - Return code 1 or 4 based on last output, not last input.
ryo1kato added a commit to ryo1kato/jq that referenced this issue Jul 29, 2018
* Set default error code to -4 in main(), Fixes jqlang#1142
* fix --exit-code with more than one object in input, Fixes jqlang#1139
    - Return code 1 or 4 based on last output, not last input.
@spirius
Copy link

spirius commented Oct 22, 2018

another workaround:

$ echo | jq -es 'if . == [] then null else .[] | your-pipe end'
$ echo $?
1

nicowilliams pushed a commit that referenced this issue Dec 18, 2018
* Set default error code to -4 in main(), Fixes #1142
* fix --exit-code with more than one object in input, Fixes #1139
    - Return code 1 or 4 based on last output, not last input.
falconindy added a commit to archlinux/asp that referenced this issue Feb 12, 2019
This reverts commit b233ace while
preserving bd3f96a.

jq, even with -e, does not exit non-zero when given an empty input. This
is apparently a known feature:

jqlang/jq#1497
jqlang/jq#1142

Thus, revert this oversimplification which leads to problems when curl
fails, or otherwise returns an empty response body.

Closes #29.
rbolsius pushed a commit to rbolsius/jq that referenced this issue Sep 12, 2019
* Set default error code to -4 in main(), Fixes jqlang#1142
* fix --exit-code with more than one object in input, Fixes jqlang#1139
    - Return code 1 or 4 based on last output, not last input.
@DrStrangepork
Copy link

This is still an issue with jq version 1.6

# echo | jq -e .; echo $?
0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants