how can i count number of entries in a json file from bash

You can easily count the number of entries in a JSON file from Bash using jq. The exact command depends on the structure of your JSON file.

1. If your JSON file is an array of objects, e.g.:

[
  {"name": "Alice"},
  {"name": "Bob"},
  {"name": "Charlie"}
]

You can count the number of entries with:

jq length file.json

or

jq '. | length' file.json

2. If your JSON file is an object with keys, e.g.:

{
  "a": {"name": "Alice"},
  "b": {"name": "Bob"}
}

You can count the number of keys with:

jq 'keys | length' file.json

3. If you want to count all objects in a nested structure, you might use:

jq '.[]' file.json | wc -l

But this works best for top-level arrays.


Summary:

  • For arrays: jq length file.json
  • For objects: jq 'keys | length' file.json