Count items in JSON array

echo '[1,2,3]' | jq '. | length'
Pipe any JSON to jq, then count with the appropiate expression and use the | length on the array

0
2018-05-03 06:34:05

These Might Interest You

  • This command loops over all indexes of the system variable array ARRAY[] and puts its content into %A. Create this array before, e.g. by set ARRAY[0]=test1 and set ARRAY[1]=test2 For using inside of a batch file, write %%A instead of %A. Show Sample Output


    -3
    FOR /F "tokens=3* delims=[]=" %A IN ('SET ARRAY[') DO ( echo %A )
    Marco · 2010-08-10 12:08:26 0
  • creates associative array from apache logs, assumes "combined" log format or similar. replace awk column to suit needs. bandwidth per ip is also useful. have fun. I haven't found a more efficient way to do this as yet. sorry, FIXED TYPO: log file should obviously go after awk, which then pipes into sort.


    1
    awk '{array[$1]++}END{ for (ip in array) print array[ip] " " ip}' <path/to/apache/*.log> | sort -n
    kevinquinnyo · 2011-11-22 03:38:21 2
  • Validates and pretty-prints the content fetched from the URL. Show Sample Output


    11
    curl -s "http://feeds.delicious.com/v2/json?count=5" | python -m json.tool | less -R
    keimlink · 2010-03-24 09:15:12 1
  • Loops over array of a system var, splits its values and puts the values into %A, %B, %C, %D, and so on. Create array before, like set ARRAY[0]=test1,100 and set ARRAY[1]=test2,200 Be sure to replace %A, %B, etc. with %%A, %%B, etc. when using this from inside of batch files. Show Sample Output


    -2
    FOR /F "tokens=3* delims=[]=," %A IN ('SET ARRAY[') DO ( echo %A -- %B )
    Marco · 2010-08-10 12:12:27 2

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: