# Ruby filter to extract JSON value

**URL:** <https://discuss.elastic.co/t/ruby-filter-to-extract-json-value/219745>\
**Category:** Logstash\
**Created:** [February 18, 2020, 9:40am UTC](https://discuss.elastic.co/t/ruby-filter-to-extract-json-value/219745 "2020-02-18T09:40:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![karlm](https://avatars.discourse-cdn.com/v4/letter/k/d2c977/32.png) [@karlm](https://discuss.elastic.co/u/karlm)\
**Post date:** [February 18, 2020, 9:40am UTC](https://discuss.elastic.co/t/ruby-filter-to-extract-json-value/219745/1 "2020-02-18T09:40:03Z")

</div>

I have an array and need to extract the latest date value, the array below looks like this

```
{
    "conversations": [
        {
            "userMessages": [
                {
                    "sbdh": {
                        "documentIdentification": {
                            "creationDate": "2020-02-17T12:04:31.388Z"
                        }
                    }
                }
            ]
        },
        {
            "userMessages": [
                {
                    "sbdh": {
                        "documentIdentification": {
                            "creationDate": "2020-02-18T12:04:31.388Z"
                        }
                    }
                }
            ]
        }
    ]
}

```

My ruby filter

```
ruby {
    code => "
        arr = event.get('[conversations]')[-1];
        event.set('document_date', arr[0].sbdh.documentIdentification.creationDate);           
    "
  }

```

return nil class error?

`Ruby exception occurred: undefined method `sbdh' for nil:NilClass`

---

<div class="post-metadata">

**Author:** ![karlm](https://avatars.discourse-cdn.com/v4/letter/k/d2c977/32.png) [@karlm](https://discuss.elastic.co/u/karlm)\
**Post date:** [February 18, 2020, 10:36am UTC](https://discuss.elastic.co/t/ruby-filter-to-extract-json-value/219745/2 "2020-02-18T10:36:14Z")

</div>

```
ruby {
    code => "
        dt = event.get('[conversations][-1][userMessages][0][sbdh][documentIdentification][creationDate]');            
        event.set('document_date', dt);           
    "
  }
```

---

<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 17, 2020, 10:36am UTC](https://discuss.elastic.co/t/ruby-filter-to-extract-json-value/219745/3 "2020-03-17T10:36:17Z")

</div>

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