Skip to content

Instantly share code, notes, and snippets.

@t0r0X
Last active August 10, 2022 19:26
Show Gist options
  • Select an option

  • Save t0r0X/52c20cbcd528956e0436426a7585840f to your computer and use it in GitHub Desktop.

Select an option

Save t0r0X/52c20cbcd528956e0436426a7585840f to your computer and use it in GitHub Desktop.
Determine JSON array length, handling errors
# 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