# Using event.get to get variable initialized to string

**URL:** <https://discuss.elastic.co/t/using-event-get-to-get-variable-initialized-to-string/312360>\
**Category:** Logstash\
**Created:** [August 18, 2022, 8:20am UTC](https://discuss.elastic.co/t/using-event-get-to-get-variable-initialized-to-string/312360 "2022-08-18T08:20:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mor\_Y](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mor_y/32/99390_2.png) [@Mor\_Y](https://discuss.elastic.co/u/Mor_Y)\
**Post date:** [August 18, 2022, 8:20am UTC](https://discuss.elastic.co/t/using-event-get-to-get-variable-initialized-to-string/312360/1 "2022-08-18T08:20:05Z")

</div>

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

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

```

The output will be:

```auto
{"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:

```auto
    
   	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)
			   end
           end
		'
   }

```

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

Thanks

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [August 18, 2022, 5:02pm UTC](https://discuss.elastic.co/t/using-event-get-to-get-variable-initialized-to-string/312360/2 "2022-08-18T17:02:26Z")

</div>

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)

```

---

<div class="post-metadata">

**Author:** ![Mor\_Y](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mor_y/32/99390_2.png) [@Mor\_Y](https://discuss.elastic.co/u/Mor_Y)\
**Post date:** [August 19, 2022, 6:32pm UTC](https://discuss.elastic.co/t/using-event-get-to-get-variable-initialized-to-string/312360/3 "2022-08-19T18:32:18Z")

</div>

Thanks, it solved my problem!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 16, 2022, 6:32pm UTC](https://discuss.elastic.co/t/using-event-get-to-get-variable-initialized-to-string/312360/4 "2022-09-16T18:32:28Z")

</div>

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