How to get array length in JQ
To calculate the length of an array in JQ, you can use the length
function. This is a simple yet powerful way to determine how many elements an array contains.
Example Input
Here is an example JSON input:
{
"fruits": ["apple", "banana", "cherry"],
"categories": {
"vegetables": ["carrot", "broccoli"]
}
}
Example Expression and Output
The following examples demonstrate how to calculate array lengths using JQ:
# Get the length of the 'fruits' array
.fruits | length
# Output:
3
# Get the length of the 'vegetables' array
.categories.vegetables | length
# Output:
2