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

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

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

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

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

@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

1 Like

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