Saving a specific array element in Logstash


I am receiving a JSON object with an array property. I would like to search the array and save only the element that matches my criteria. My input looks like this:

  "identifier": [
    { "system" : "Source1", "value" : "TheValueIDontWant"},
    { "system" : "Source2", "value" : "TheValueIWant"}

and I would like my output to look like this:

  "SourceID": "TheValueIWant"

So in this case, I want to search the identifier array for the element which has Source2 as the system and save its corresponding value to my new property.

Is there a way to do this in Logstash?

You could try something like this (which I have not tested)

ruby {
    code => '
        ids = event.get("identifier")
        if ids.is_a? Array
            ids.each { |x|
                if x["system"] == "Source2"
                    event.set("SourceID", x["value"])

Thank you, that's exactly what I was looking for and the code didn't need any changes. I was battling to find a nice example for using ruby in the .conf and you've just given me one.

I owe you a beer!

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