# Undefined method \`each' for nil:NilClass

**URL:** https://discuss.elastic.co/t/undefined-method-each-for-nil-nilclass/180976
**Category:** Logstash
**Created:** [May 14, 2019, 10:17am UTC](https://discuss.elastic.co/t/undefined-method-each-for-nil-nilclass/180976 "2019-05-14T10:17:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![souf\_el\_badraoui](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/souf_el_badraoui/32/47746_2.png) [@souf\_el\_badraoui](https://discuss.elastic.co/u/souf_el_badraoui)
#### Post date: [May 14, 2019, 10:17am UTC](https://discuss.elastic.co/t/undefined-method-each-for-nil-nilclass/180976/1 "2019-05-14T10:17:38Z")

</div>

Hi,  
We are trying to flatten a json array with the following ruby code in Logstash

```
ruby {
  init => '@ignore = ["@metadata", "@version" , "beat", "type", "tags"]'
  code => '
    event.get("[EventData]").each { | k, v| event.set(k, v) }
    event.remove("EventData")
    event.remove("user")
    s = ""
    event.to_hash.each { |k, v|
      unless @ignore.include?(k)
        s = s + "#{k}=\"#{v}\" "
      end
      event.set("rawMessage", s.chomp(","))
    }
  '
}

```

And we get the following error:  
Ruby exception occurred: undefined method `each' for nil:NilClass  
Someone can help us ?

---

<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: [May 14, 2019, 12:32pm UTC](https://discuss.elastic.co/t/undefined-method-each-for-nil-nilclass/180976/2 "2019-05-14T12:32:16Z")

</div>

> [@souf\_el\_badraoui](#):
>
> Ruby exception occurred: undefined method `each' for nil:NilClass

That would very much suggest that there is no EventData field on the message.

---

<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: [June 11, 2019, 12:32pm UTC](https://discuss.elastic.co/t/undefined-method-each-for-nil-nilclass/180976/3 "2019-06-11T12:32:17Z")

</div>

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