# Event.set and nested filed

**URL:** <https://discuss.elastic.co/t/event-set-and-nested-filed/249684>\
**Category:** Logstash\
**Created:** [September 23, 2020, 4:59pm UTC](https://discuss.elastic.co/t/event-set-and-nested-filed/249684 "2020-09-23T16:59:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![arp220](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arp220/32/84666_2.png) [@arp220](https://discuss.elastic.co/u/arp220)\
**Post date:** [September 23, 2020, 4:59pm UTC](https://discuss.elastic.co/t/event-set-and-nested-filed/249684/1 "2020-09-23T16:59:44Z")

</div>

Hi.  
I have this config:

```auto
input {
  file {
     path => "/opt/logs/sb_log.json"
     sincedb_path => "/etc/logstash/.sincedb_sblog"
     start_position => "beginning"
   }
}
filter{
  json {
        source => "message"
  }
  json {
    source => "payload"
    target => "parsepaylaod"
  }
  json {
    source => "headers"
    target => "parseheaders"
  }
  mutate {
    convert => ["parentId","integer"]
    convert => ["currentNano","integer"]
    convert => ["createTimeMillis","integer"]
    convert => ["elapse","float"]
  }

  ruby {
      code => "event.set('diff',(event.get(['_source']['parseheaders']['kafka_receivedTimestamp']) - event.get('createTimeMillis')))" 
   }
 mutate {
        remove_field => ["@version"]
  }

if "_jsonparsefailure" in [tags] { drop {} }
}

output {
     elasticsearch {
        hosts => ["http://192.168.0.39:9200"]
        user => "user"
        password => "pass"
        index => "logstash_index_beta"
    }
}

```

I have two questions, my syntax in ruby is correct? Do I cat get` parseheaders.kafka_receivedTimestamp` value this way?

```auto
code => "event.set('diff',(event.get(['_source']['parseheaders']['kafka_receivedTimestamp']) - event.get('createTimeMillis')))" 

```

another this `parseheaders.kafka_receivedTimestamp` and `createTimeMillis` is integer type but I get this error

```auto
Sep 23 20:22:18 app1 logstash[31508]: [2020-09-23T20:22:18,571][ERROR][logstash.filters.ruby][main][c86aa057dfd82fb3bc82c6597913f6166b84156506f117224d0d3540263942d5] Ruby exception occurred: no implicit conversion of String into Integer
Sep 23 20:22:18 app1 logstash[31508]: [2020-09-23T20:22:18,573][ERROR][logstash.filters.ruby][main][c86aa057dfd82fb3bc82c6597913f6166b84156506f117224d0d3540263942d5] Ruby exception occurred: no implicit conversion of String into Integer
Sep 23 20:22:18 app1 logstash[31508]: [2020-09-23T20:22:18,574][ERROR][logstash.filters.ruby][main][c86aa057dfd82fb3bc82c6597913f6166b84156506f117224d0d3540263942d5] Ruby exception occurred: no implicit conversion of String into Integer

```

thanks a lot for your help.

---

<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:** [September 23, 2020, 5:22pm UTC](https://discuss.elastic.co/t/event-set-and-nested-filed/249684/2 "2020-09-23T17:22:08Z")

</div>

> [@arp220](#):
>
> ```auto
> ruby {
> code => "event.set('diff',(event.get(['_source']['parseheaders']['kafka_receivedTimestamp']) - event.get('createTimeMillis')))" 
> }
> 
> ```

\_source is an elasticsearch artifact used to show you the document that was indexed. It does not generally exist in logstash. You probably want

```
ruby {
    code => "event.set('diff',(event.get('[parseheaders][kafka_receivedTimestamp]').to_i - event.get('createTimeMillis').to_i))" 
}

```

If those fields are strings in logstash you need to explicitly call .to\_i to convert them to integers (or use mutate+convert before the ruby filter) so that the - operator will work.

---

<div class="post-metadata">

**Author:** ![arp220](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arp220/32/84666_2.png) [@arp220](https://discuss.elastic.co/u/arp220)\
**Post date:** [September 23, 2020, 6:38pm UTC](https://discuss.elastic.co/t/event-set-and-nested-filed/249684/3 "2020-09-23T18:38:01Z")

</div>

thanks, @Badger, this problem solved, but why in kibana I can't create visualize for this filed `diff`?  
How do I save this field so that it can be used in visualize Kibana?

thanks.

---

<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:** [September 23, 2020, 6:44pm UTC](https://discuss.elastic.co/t/event-set-and-nested-filed/249684/4 "2020-09-23T18:44:20Z")

</div>

Did you refresh the index pattern?

---

<div class="post-metadata">

**Author:** ![arp220](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arp220/32/84666_2.png) [@arp220](https://discuss.elastic.co/u/arp220)\
**Post date:** [September 23, 2020, 6:47pm UTC](https://discuss.elastic.co/t/event-set-and-nested-filed/249684/5 "2020-09-23T18:47:24Z")

</div>

I love you @Badger 🌻

---

<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:** [October 21, 2020, 6:47pm UTC](https://discuss.elastic.co/t/event-set-and-nested-filed/249684/6 "2020-10-21T18:47:37Z")

</div>

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