4

I have around 3K JSON files inside a directory and each file contains approximately 250 (+/-) JSON elements inside it, I want to count the total number of JSON elements from those files as a sum.

Currently using jq like below command which returns numbers in each line but I want to count total by adding them,

jq length *.json

Current Output,

250
248
250
240
...
...
250

Expected Output (approx),

600530

2 Answers 2

8

Using jq only:

jq -s 'map(length) | add' ./*.json

-s/--slurp makes jq read its input as a single array, running the specified filter only once against it. map is used to run length for each element of that virtual array, returning an array of numbers, and add finally sums them.

To also make sure not to hit the command line length limit (but note that this would also recursively process files in subdirectories1):

find . -name "*.json" -exec jq 'length' {} + | jq -s 'add'

Found files are passed to jq 'length' in batches whose size depends on the maximum command line length allowed on your system. Since find may run jq more than once, making it slurp its input won't reliably work and its output is piped to a second (slurping) jq instead.


1 Several Q/As on this site show how to prevent find from descending into directories; for instance, Using "find" non-recursively?

2

You can use simple awk command i.e.

jq length ./*.json | awk '{sum=sum+$0} END{print sum}'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .