370

I'm getting this kind of JSON reply from a curl command:

[
  {
    "cid": 49,
    "pyn": "yi4",
    "hans": "亿",
    "hant": "億",
    "tid": 68,
    "l10n": "cent million",
    "pid": 1,
    "pos": "num",
    "pos_txt": ""
  },
  {
    "cid": 50,
    "pyn": "yi4",
    "hans": "亿",
    "hant": "億",
    "tid": 69,
    "l10n": "100 millions",
    "pid": 1,
    "pos": "num",
    "pos_txt": ""
  }
]

How can I count the number of items in the array (here 2), using Bash or a command line (e.g. underscore) ?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178

7 Answers7

627

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.

peak
  • 105,803
  • 17
  • 152
  • 177
Ken
  • 7,847
  • 1
  • 21
  • 20
  • 4
    Your initial `jq` code (`.[]`) return the length of each `object` in the root array, while I'm looking for the length of the root array itself. Need to fix to `.` – Édouard Lopez Jan 26 '14 at 09:52
  • 53
    If your root is not an array but an object with a key that contains an array, i.e. { "key": [elem1, elem2] } , then you can use use `jq '.[] | length' file.json` – Alex Bitek Jan 27 '15 at 15:10
  • 2
    Another useful option for that @MnemonicFlow is `jq map_values(length) file.json` . That will give you the keys as well. – Paulo Casaretto Dec 09 '16 at 19:06
  • 17
    And if your input is made of independent objects instead of a single array, you'd use the `-s` or `--slurp` option, which collects them into an array while reading: `jq -s length file.json` – hemflit Jul 02 '18 at 10:34
  • `. | ` is not required, `jq 'length' ... ` is enough – ssc Sep 02 '18 at 14:39
  • @bitek Either I understood you wrong, or you're mistaken. `echo '{"key":[1,"asdf"]}' | jq '.[] | length'` gives an error. What produces expected result to me is `echo '{"key":[1,"asdf"]}' | jq '.key | length'`. – x-yuri Sep 30 '18 at 01:39
  • Bitek's method (`.[]`) works for me but it's just a shorthand for `.key`, usable when the object is only a wrapper around an array (in which case the key is basically arbitrary). – Sinjai Jun 23 '22 at 04:31
87

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
$ 
Mr. Lance E Sloan
  • 3,297
  • 5
  • 35
  • 50
yuvilio
  • 3,795
  • 32
  • 35
86

The shortest expression is

curl 'http://…' | jq length
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
nikolay
  • 2,565
  • 1
  • 21
  • 13
19

If the JSON is being read from a file, try this -

number_of_objects=`jq '. | length' json_file_name.json`
echo $number_of_objects

If the JSON array is inside a key in the JSON as shown below -

{
  "fruits": [
    "apples",
    "oranges",
    "pears"
  ]
}

try this -

number_of_objects=`jq '.fruits | length' json_file_name.json`
echo $number_of_objects

(You'll have to download jq for this solution to work)

Dhruv Saraswat
  • 858
  • 9
  • 13
6

Assume this structure:

(stored in a variable named json)

{
    "results": [
        { "id": 1 }
        { "id": 2 }
    ]

}

If you're looking to count how many are in the results array, the command is:

$json | jq '.results | length'
Kellen Stuart
  • 7,775
  • 7
  • 59
  • 82
5

A simple solution is to install jshon library :

jshon -l < /tmp/test.json
2
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
3

try qic. it works like jq/jello. qic support interactive mode as well.

cat test.json | qic "len(_)"

https://walkerever.github.io/qic/

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Y W
  • 31
  • 1