Using event.get to get variable initialized to string

Hi, I want to add some fields that will have the same values of others but with different names:
for Example the input json:

{"Environment" : "x", "Name" : "y"}

The output will be:

{"Environment" : "x", "Name" : "y", "environment" : "x ", "name" : "y"}

Because I have a lot of fields to add I tried to do some mapping before and use ruby:

   	ruby {
		code => '
			mapping = {"Environment":"environment", "Name": "name"}
			mapping.each do |old_field, new_field|
			   old_field_val = event.get(old_field)
              if old_field_val
				event.set(new_field, old_field_val)

It seems like the event.get can't get variable but has to get string.
Is there any other way to do so?


You should be getting the error message "Ruby exception occurred: no implicit conversion of Symbol into String". You can fix that using

old_field_val = event.get(old_field.to_s)

Thanks, it solved my problem!

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