How to assign one value to another

Hi ,
I am new to logstash
I have below data in JSON.
"attributes":
[
{ "name": "test1",
"value": "test1_value" },
{ "name": "test2",
"value": "test2_value" }
]
I want this data in below format :
"attributes":
[
{ "test1": "test1_value" },
{ "test2": "test2_value" }
]
how can we do this in logstash config?

You would do it in a ruby filter. I have not tested it, but you could try this:

ruby {
    code => '
        a = event.get("attributes")
        if a.is_a? Array
            newA = []
            a.each { |x|
                 newA << { x["name"] => x["value"] }
            }
            event.set("attributes", newA)
        end
    '
}

Hi @Badger ,
Thanks for your response.
My input Json is something like this .
{
"field1":value1,
"attributes": [{ "name": "test1", "value": "test_value1" },{ "name": "test2", "value": "test_value2" }]
}
and config as below:

input {
file
{
type => "json"
codec => "json"
path => "C:/logstash-7.6.0/input/sample*.json"
start_position => "beginning"
}
}
filter {

		mutate {
			add_field => { 
									"sample_Number"=>"%{[field1]}"									
									"Attributes"=>"%{[attributes]}"
									
							}
				}

ruby {
code => '
a = event.get("Attributes")
if a.is_a? json
newA = {}
a.each { |x|
newA << { x["name"] => x["value"] }
}
event.set("Attributes", newA)
end
'
}

			prune {
						whitelist_names =>["sample_Number","Attributes"]
  					}
}

output {
stdout { }
elasticsearch {
index => "test_index"
hosts => ["http://localhost:9200/"]
action => "create"
}
}

It is not working as expected.

This will never be true, so the ruby code will not do anything. Attributes is an Array.

I tried with if a.is_a? Array. but still same issue
It is passing entire json as it is.

it worked now.
issue was ..
before below code, I was copying data from attribute field to another fields. after removing that it worked
ruby {
code => '

    a = event.get("attributes")
    if a.is_a? Array 
        newA = []
        a.each do |x|
             newA << { x["name"] => x["value"] }
        end
        event.set("attributes", newA)
		
    end
'

}

Thanks @Badger

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