How to count items in JSON object using command line? How to count items in JSON object using command line? bash bash

How to count items in JSON object using command line?


Just throwing another solution in the mix...

Try jq, a lightweight and flexible command-line JSON processor:

jq length /tmp/test.json

Prints the length of the array of objects.


The shortest expression is

curl 'http://…' | jq length


You can also use jq to track down the array within the returned json and then pipe that in to a second jq call to get its length. Suppose it was in a property called records, like {"records":[...]}.

$ curl https://my-source-of-json.com/list | jq -r '.records | length'2$