jq length

Here’s an example that showcases how to use the length filter in jq:

echo '[1, 2, 3]' | jq length

In this example, we have a simple JSON array [1, 2, 3]. By piping this JSON data to jq length, we can obtain the length of the array. The output will be 3, as the array contains three elements.

Likewise, if we have a JSON object and want to find the number of keys, we can also use the length filter. Here’s an example:

echo '{"name": "John", "age": 25, "country": "USA"}' | jq keys | jq length

In this example, we have a JSON object with three key-value pairs. By first using jq keys to obtain the array of keys and then piping it to jq length, we can get the number of keys in the object. The output will be 3.

Using the length filter in jq allows us to easily determine the size of JSON arrays or the number of keys in JSON objects, providing us with valuable information when working with JSON data.