Last active
August 10, 2022 19:26
-
-
Save t0r0X/52c20cbcd528956e0436426a7585840f to your computer and use it in GitHub Desktop.
Determine JSON array length, handling errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # See https://stedolan.github.io/jq/manual/ | |
| echo '{}' | jq 'if isempty(. | arrays) then "error" else . | arrays | length end' | |
| # error | |
| echo '[]' | jq 'if isempty(. | arrays) then "error" else . | arrays | length end' | |
| # 0 | |
| echo '{}' | jq 'if isempty(. | arrays) then "input not an array"|halt_error(1) else . | arrays | length end' | |
| # input not an array | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment