# Parsing an array in Ruby

**URL:** https://discuss.elastic.co/t/parsing-an-array-in-ruby/121254
**Category:** Logstash
**Created:** [February 23, 2018, 4:44pm UTC](https://discuss.elastic.co/t/parsing-an-array-in-ruby/121254 "2018-02-23T16:44:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Karanushah.1992](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@Karanushah.1992](https://discuss.elastic.co/u/Karanushah.1992)
#### Post date: [February 23, 2018, 4:44pm UTC](https://discuss.elastic.co/t/parsing-an-array-in-ruby/121254/1 "2018-02-23T16:44:36Z")

</div>

Hi,

I have an array field which has epoch time values. I am trying to use Ruby filter to parse it. I am getting a syntax error. I am new to Ruby so I am unable to spot the error. Can you help me out. Below is my code in Ruby

ruby {  
code =\> {'  
event.get("timestamp").each\_with\_index { |x, i| event["timestamp1#{i}"] =  
Time.at(x) }  
'}  
}

---

<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 23, 2018, 5:38pm UTC](https://discuss.elastic.co/t/parsing-an-array-in-ruby/121254/2 "2018-02-23T17:38:23Z")

</div>

```auto
ruby {
  code => '
    event.get("timestamp").each_with_index { |x, i| event.set( "timestamp1#{i}" , Time.at(x.to_i)) }
  '
}

```

---

<div class="post-metadata">

### Author: ![Karanushah.1992](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@Karanushah.1992](https://discuss.elastic.co/u/Karanushah.1992)
#### Post date: [February 23, 2018, 6:18pm UTC](https://discuss.elastic.co/t/parsing-an-array-in-ruby/121254/3 "2018-02-23T18:18:28Z")

</div>

HI @Badger , the event.set( "timestamp1#{i}" creates multiple fields which I do not want.

my timestamp field looks like this  
"timestamp": [  
1518044400000,  
1518087600000,  
1518109200000,  
1518130800000,  
1518174000000,  
1518195600000,  
1518217200000,  
1518433200000,  
1518454800000,  
1518476400000,  
1518519600000,  
1518541200000,  
1518562800000,  
1518606000000,  
1518627600000  
]

I want to create new field timestamp 1 which looks like this

timestamp1: [  
February 21st 2018, 09:49:46.420 ,  
February 21st 2018, 09:49:46.420 ,  
February 21st 2018, 09:49:46.420 ,  
February 21st 2018, 09:49:46.420 ,  
]

---

<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 23, 2018, 6:48pm UTC](https://discuss.elastic.co/t/parsing-an-array-in-ruby/121254/4 "2018-02-23T18:48:13Z")

</div>

```auto
  code => '
    timestamps = Array.new
    event.get("timestamp").each_with_index { |x, i| timestamps.push(Time.at(x.to_i / 1000)) }
    event.set( "IsThisOK" , timestamps)
  '

```

Does that put the right data into IsThisOK? You don't really need with\_index at this point, and there are lots of other ways to do the init and push for the array, but I think it works.

---

<div class="post-metadata">

### Author: ![Karanushah.1992](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@Karanushah.1992](https://discuss.elastic.co/u/Karanushah.1992)
#### Post date: [February 23, 2018, 7:04pm UTC](https://discuss.elastic.co/t/parsing-an-array-in-ruby/121254/5 "2018-02-23T19:04:40Z")

</div>

Hi @Badger . it does what I wanted to try. I am not sure will I be able to use this field in Kibana as a date field

---

<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 23, 2018, 7:04pm UTC](https://discuss.elastic.co/t/parsing-an-array-in-ruby/121254/6 "2018-03-23T19:04:54Z")

</div>

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