How to get both key and value from JOSN uisng ruby

I want both key and value from json using ruby in logstash, By using the belwo snippet, able to get only value not key. i need key also. Can you please help me:


ruby {
code => '
hostId = ""
serverName = ""
event.get("[result][entities]").each { |x|
x.each { |key, value|
event.set("hostId", key)
event.set("serverName", value)

Desired Output:


Can any one please help me

        code => '
            event.get("[result][entities]").each { |k,v|
                event.set("hostId", k)
                event.set("serverName", v)

Thanks for replying,

I have one more question can you please help me:


Desired Output:

Can you please help me how to solve

Do you mean you want to split an event into two different events?

Yeah I tried with ruby code but only last index value I'm getting

Tried like this :

ruby {
code => '
dataPointTime = ""
dataPointValue = ""
dataPoints_size = event.get("[result][dataPoints][HOST-B89B05174C5B9765]").size
dataPoints_size.times do |index|
dataPointTime += event.get("[result][dataPoints][HOST-B89B05174C5B9765][#{index}][0]")+ ","
dataPointValue += event.get("[result][dataPoints][HOST-B89B05174C5B9765][#{index}][1]")+ ","
event.set("dataPointTime", dataPointTime)
event.set("dataPointValue", dataPointValue)

You do not need to use ruby. You can use a mutate filter.

    split { field => "[result][dataPoints][HOST-B89B05174C5B9765]" }
    mutate { add_field => { "dataPointTime" => "%{[result][dataPoints][HOST-B89B05174C5B9765][0]}" } }

But I have around to 10 datapoints in the host, so I need to write dynamically. Can you please help me.

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