1

I have a bash script that defines a function that returns dynamic JSON as a result:

# inside bash script
numInstances=getInstances();

Where an example of the JSON returned by getInstances() is:

[
  [
    {
      "id": 193932,
      "name": "foobaz"
    },
    {
      "id": 28348,
      "name": "fizzbuzz"
    }
  ]
]

It's important to note that although the JSON is dynamic, it will always be a nested JSON array of the form:

[
  [
     <json list here; this is what I need a count of>
  ]
]

How can I pipe jq to the output of getInstances() so that it will parse the JSON and return the number of instances inside the nested JSON array? For example, in the above output, there are 2 "instances" inside the nested JSON array. So I would want numInstances to be set to 2, etc.

1 Answer 1

4
.[0]|length

would suffice, but is not very robust against a violation of assumptions.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, so: numInstances=getInstances() | jq.[0]|length?
numInstances=$(getInstances | jq '.[0]|length')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.