292

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
  • 77,979
  • 13
  • 71
  • 125
Édouard Lopez
  • 36,430
  • 24
  • 114
  • 170

6 Answers6

533

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
  • 88,177
  • 15
  • 123
  • 150
Ken
  • 6,887
  • 1
  • 19
  • 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
  • 33
    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
  • 11
    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
74

The shortest expression is

curl 'http://…' | jq length
Édouard Lopez
  • 36,430
  • 24
  • 114
  • 170
nikolay
  • 2,366
  • 20
  • 13
48

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
  • 2,970
  • 5
  • 31
  • 48
yuvilio
  • 3,385
  • 31
  • 33
8

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
  • 538
  • 4
  • 10
4

A simple solution is to install jshon library :

jshon -l < /tmp/test.json
2
Édouard Lopez
  • 36,430
  • 24
  • 114
  • 170
2

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

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

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

Y W
  • 21
  • 1