# How to create new array by using existing list of strings field in logstash ruby filter

**URL:** <https://discuss.elastic.co/t/how-to-create-new-array-by-using-existing-list-of-strings-field-in-logstash-ruby-filter/330761>\
**Category:** Logstash\
**Created:** [April 25, 2023, 6:27pm UTC](https://discuss.elastic.co/t/how-to-create-new-array-by-using-existing-list-of-strings-field-in-logstash-ruby-filter/330761 "2023-04-25T18:27:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Disha\_Bodade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/disha_bodade/32/84522_2.png) [@Disha\_Bodade](https://discuss.elastic.co/u/Disha_Bodade)\
**Post date:** [April 25, 2023, 6:27pm UTC](https://discuss.elastic.co/t/how-to-create-new-array-by-using-existing-list-of-strings-field-in-logstash-ruby-filter/330761/1 "2023-04-25T18:27:44Z")

</div>

Hi Team,  
I have three arrays created from xml in logstash

```auto
content.REFERENCE: [PXXXX, TECHNICAL_SUPPORT]
content.ROOT: [INTERNAL_PRODUCT_OR_APPLICATION, TOPICS]

```

I have to create a result array from above inputs  
if root array has product then create array productid : [PXXXX],  
if root array has topics then create array producttopic: [TECHNICAL\_SUPPORT]

I created below code

```auto
ruby {
code => "
content_root = event.get('[content][ROOT]')
 content_refkey = event.get('[content][REFERENCE]')
                
producttopics = []
productid = []
                                                

if content_root.is_a? Hash
  i=0
  content_root.each do |root|
   if root == 'PRODUCT'
       productid << content_refkey[i]
   elsif root == 'TOPICS'
       producttopics << content_refkey[i]
   end
   i=i+1
  end
end
event.set('productid', productid)
event.set('producttopics', producttopics)
 "
 }

```

but its showing error as the input fields are not array type in logstash. those array automatically created by xml filter from xml source.

is it considering it as a string? Please let me know.

Thanks,  
Disha

---

<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:** [April 25, 2023, 6:43pm UTC](https://discuss.elastic.co/t/how-to-create-new-array-by-using-existing-list-of-strings-field-in-logstash-ruby-filter/330761/2 "2023-04-25T18:43:50Z")

</div>

Impossible to say without seeing an example of the data (use `output { stdout { codec => rubydebug } }`). How is the XML filter configured?

---

<div class="post-metadata">

**Author:** ![Disha\_Bodade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/disha_bodade/32/84522_2.png) [@Disha\_Bodade](https://discuss.elastic.co/u/Disha_Bodade)\
**Post date:** [April 28, 2023, 4:57am UTC](https://discuss.elastic.co/t/how-to-create-new-array-by-using-existing-list-of-strings-field-in-logstash-ruby-filter/330761/3 "2023-04-28T04:57:00Z")

</div>

@Badger,  
Thanks for your suggestion. When I enabled rubydebug, I got the proper syntax of array logstash was creating, and I created a script based on that, its working fine.

Thanks,  
Disha

---

<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:** [May 26, 2023, 4:57am UTC](https://discuss.elastic.co/t/how-to-create-new-array-by-using-existing-list-of-strings-field-in-logstash-ruby-filter/330761/4 "2023-05-26T04:57:22Z")

</div>

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