# How to parse json array to get object count

**URL:** <https://discuss.elastic.co/t/how-to-parse-json-array-to-get-object-count/170409>\
**Category:** Logstash\
**Created:** [February 28, 2019, 9:37pm UTC](https://discuss.elastic.co/t/how-to-parse-json-array-to-get-object-count/170409 "2019-02-28T21:37:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pcrai](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@pcrai](https://discuss.elastic.co/u/pcrai)\
**Post date:** [February 28, 2019, 9:37pm UTC](https://discuss.elastic.co/t/how-to-parse-json-array-to-get-object-count/170409/1 "2019-02-28T21:37:19Z")

</div>

I have given one json log file as input in logstash and getting corresponding mapping in kibana but I want to count element available in json array object. need help to get that.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [February 28, 2019, 9:51pm UTC](https://discuss.elastic.co/t/how-to-parse-json-array-to-get-object-count/170409/2 "2019-02-28T21:51:38Z")

</div>

What do you want to count?

---

<div class="post-metadata">

**Author:** ![pcrai](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@pcrai](https://discuss.elastic.co/u/pcrai)\
**Post date:** [February 28, 2019, 9:52pm UTC](https://discuss.elastic.co/t/how-to-parse-json-array-to-get-object-count/170409/3 "2019-02-28T21:52:28Z")

</div>

total number of objects in my tracks array

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [February 28, 2019, 10:35pm UTC](https://discuss.elastic.co/t/how-to-parse-json-array-to-get-object-count/170409/4 "2019-02-28T22:35:11Z")

</div>

Create a file called countObjects.rb that contains this

```auto
def register(params)
    @field = params['field']
    @target = params['target']
end

def countObjects(object)
    c = 0
    if object
        c = 1
        if object.kind_of?(Array)
            object.each { |x| c += countObjects(x) }
        elsif object.kind_of?(Hash)
            object.each { |k, v| c += countObjects(v) }
        end
    end
    c
end

def filter(event)
    event.set(@target, countObjects(event.get(@field)))
    [event]
end

```

You can call it using

```
    ruby {
        path => "/home/user/countObjects.rb"
        script_params => { "field" => "someField" "target" => "count" }
    }

```

which will count the number of object in someField and add a count field to the event that contains the number.

---

<div class="post-metadata">

**Author:** ![pcrai](https://avatars.discourse-cdn.com/v4/letter/p/c57346/32.png) [@pcrai](https://discuss.elastic.co/u/pcrai)\
**Post date:** [March 1, 2019, 11:21pm UTC](https://discuss.elastic.co/t/how-to-parse-json-array-to-get-object-count/170409/5 "2019-03-01T23:21:18Z")

</div>

it showing me 0.not giving count

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 29, 2019, 11:21pm UTC](https://discuss.elastic.co/t/how-to-parse-json-array-to-get-object-count/170409/6 "2019-03-29T23:21:25Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
