
Using JSON as a data source can be quite powerful, but what can take it to the next level is using a program that can process the JSON data to manipulate it or to simply verify how many array elements exist. This is all possible and more with the use of an open source lightweight command-line program called jq. The jq application is quite accessible being available on a variety of operating systems, FreeBSD, Linux, macOS, Solaris and Windows. Let’s begin the journey of exploring how to count JSON array elements.
What is jq?
A JavaScript Object Notation (JSON) command-line processor that provides the ability to slice, filter, map and manipulate the data structure with simplicity.
JSON String: Count Array Elements
Using the built-in length function of jq we can count the array elements found in a simple JSON string that contains a list of website addresses.
$ echo '[{"Adamsdesk": "https://www.adamsdesk.com/"},{"Adamsdesk KB": "https://kb.adamsdesk.com/"}]' | jq '. | length'
2
A File: Count Array Elements
Here are the contents of the file “example.json” that contains a list of countries and cities. Let’s count the root array elements.
{
"countries": [
{
"id": 1,
"name": "Canada",
"ISO-3166-1-alpha-2": "CA"
},
{
"id": 2,
"name": "United States of America",
"ISO-3166-1-alpha-2": "US"
}
],
"cities": [
{
"name": "Los Angeles",
"countries_id": 2
},
{
"name": "Vancouver",
"countries_id": 1
},
{
"name": "Toronto",
"countries_id": 1
}
]
}
$ jq '. | length' example.json
2
Nested Array: Count Array Elements
The nested array has a list of various city names, let’s count how many array elements are associated to cities.
echo '{"cities":[{"name":"Los Angeles"},{"name":"Vancouver"},{"name":"Toronto"}]}' | jq '.cities | length'
3
Remote JSON Data: Count Array Elements
This example queries the Mastodon account Adamsdesk profile metadata on Fosstodon using the Mastodon API and then will count the total array elements found under the “emojis” key.
$ curl "https://fosstodon.org/api/v1/accounts/lookup?acct=adamsdesk" | jq '.emojis | length'
21
For illustration purposes here are the contents of the query data with the beginning and end truncated so that only the “emojis” data is shown.
"emojis": [
{
"shortcode": "arch",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/090/050/original/ae5ab97b259fe17d.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/090/050/static/ae5ab97b259fe17d.png",
"visible_in_picker": true
},
{
"shortcode": "alpine",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/218/777/original/82d920d583221b7e.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/218/777/static/82d920d583221b7e.png",
"visible_in_picker": true
},
{
"shortcode": "linux",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/025/115/original/077a5cfa08a16bcb.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/025/115/static/077a5cfa08a16bcb.png",
"visible_in_picker": true
},
{
"shortcode": "openbsd",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/059/615/original/db208ca8502874ee.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/059/615/static/db208ca8502874ee.png",
"visible_in_picker": true
},
{
"shortcode": "android",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/025/102/original/64ec1c48b5f213fe.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/025/102/static/64ec1c48b5f213fe.png",
"visible_in_picker": true
},
{
"shortcode": "terminal",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/025/127/original/398662c04f1d032f.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/025/127/static/398662c04f1d032f.png",
"visible_in_picker": true
},
{
"shortcode": "AntennaPod",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/720/381/original/9346145bb110c8e3.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/720/381/static/9346145bb110c8e3.png",
"visible_in_picker": true
},
{
"shortcode": "firefox",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/010/378/original/6710d28f8a2cdc05.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/010/378/static/6710d28f8a2cdc05.png",
"visible_in_picker": true
},
{
"shortcode": "gimp",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/025/111/original/592f1bf79e1af1d3.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/025/111/static/592f1bf79e1af1d3.png",
"visible_in_picker": true
},
{
"shortcode": "gitea",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/702/389/original/650b0e605850f306.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/702/389/static/650b0e605850f306.png",
"visible_in_picker": true
},
{
"shortcode": "gnomewhite",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/010/367/original/gnomewhite.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/010/367/static/gnomewhite.png",
"visible_in_picker": true
},
{
"shortcode": "inkscape",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/083/940/original/2225d4cb86fd6e88.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/083/940/static/2225d4cb86fd6e88.png",
"visible_in_picker": true
},
{
"shortcode": "thunderbird",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/010/377/original/4bc6f0caa347f85a.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/010/377/static/4bc6f0caa347f85a.png",
"visible_in_picker": true
},
{
"shortcode": "ublockorigin",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/716/461/original/9fe02b7d9ec3397c.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/716/461/static/9fe02b7d9ec3397c.png",
"visible_in_picker": true
},
{
"shortcode": "duckduckgo",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/010/368/original/duckduckgo.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/010/368/static/duckduckgo.png",
"visible_in_picker": true
},
{
"shortcode": "fsf",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/084/358/original/d1411f3764516b0f.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/084/358/static/d1411f3764516b0f.png",
"visible_in_picker": true
},
{
"shortcode": "fedi",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/691/240/original/fce8be1e4468ce58.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/691/240/static/fce8be1e4468ce58.png",
"visible_in_picker": true
},
{
"shortcode": "osm",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/177/141/original/0ccb7bb1bc8563de.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/177/141/static/0ccb7bb1bc8563de.png",
"visible_in_picker": true
},
{
"shortcode": "tetris",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/100/768/original/5148c03e43140252.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/100/768/static/5148c03e43140252.png",
"visible_in_picker": true
},
{
"shortcode": "raspberrypi",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/015/409/original/raspberry-pi.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/015/409/static/raspberry-pi.png",
"visible_in_picker": true
},
{
"shortcode": "startrek",
"url": "https://cdn.fosstodon.org/custom_emojis/images/000/129/517/original/26b6b8b0066163ac.png",
"static_url": "https://cdn.fosstodon.org/custom_emojis/images/000/129/517/static/26b6b8b0066163ac.png",
"visible_in_picker": true
}
]
References
- Command-line Interface, Wikipedia
- ISO 3166-1 alpha-2, Wikipedia
- jq
- JSON, Wikipedia