469

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) ?

4
  • Is JavaScript solution, okay for you? Commented Jan 24, 2014 at 13:51
  • Through NPM module yes. Otherwise, no. Commented Jan 24, 2014 at 13:53
  • Check my solution. That needs no npm. Plain JavaScript. Commented Jan 24, 2014 at 13:55
  • 3
    I'm in a bash context, not web Commented Jan 24, 2014 at 13:59

7 Answers 7

743

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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 .
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
Another useful option for that @MnemonicFlow is jq map_values(length) file.json . That will give you the keys as well.
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
. | is not required, jq 'length' ... is enough
|
129

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
$ 

Comments

96

The shortest expression is

curl 'http://…' | jq length

Comments

32

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)

Comments

20

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'

Comments

5

A simple solution is to install jshon library :

jshon -l < /tmp/test.json
2

Comments

2

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

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.