How to count items in JSON object using command line?
Solution 1:
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.
Solution 2:
The shortest expression is
curl 'http://…' | jq length
Solution 3:
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
$