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.
CodePudding user response:
.[0]|length
would suffice, but is not very robust against a violation of assumptions.