# How to pass ruby variable to logstash field?

**URL:** <https://discuss.elastic.co/t/how-to-pass-ruby-variable-to-logstash-field/80664>\
**Category:** Logstash\
**Created:** [March 30, 2017, 11:33am UTC](https://discuss.elastic.co/t/how-to-pass-ruby-variable-to-logstash-field/80664 "2017-03-30T11:33:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![laptop\_data](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@laptop\_data](https://discuss.elastic.co/u/laptop_data)\
**Post date:** [March 30, 2017, 11:33am UTC](https://discuss.elastic.co/t/how-to-pass-ruby-variable-to-logstash-field/80664/1 "2017-03-30T11:33:08Z")

</div>

Hi, I'm using ruby filter in my logstash conf file where it will produce some output into a variable which is json data, so that i will filter that data using json filter. But the problem is i can't able to pass that variable out side the ruby filter. Similialrly how should i pass my logstash field to ruby variable.

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [March 30, 2017, 12:01pm UTC](https://discuss.elastic.co/t/how-to-pass-ruby-variable-to-logstash-field/80664/2 "2017-03-30T12:01:25Z")

</div>

See [https://www.elastic.co/guide/en/logstash/current/event-api.html](https://www.elastic.co/guide/en/logstash/current/event-api.html) for how to read from and write to event fields.

---

<div class="post-metadata">

**Author:** ![laptop\_data](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@laptop\_data](https://discuss.elastic.co/u/laptop_data)\
**Post date:** [March 30, 2017, 12:46pm UTC](https://discuss.elastic.co/t/how-to-pass-ruby-variable-to-logstash-field/80664/3 "2017-03-30T12:46:28Z")

</div>

Hi @magnusbaeck, Thanks for your replay. That links provides only pass logstash fields to ruby, I also need to get ruby variables to logstash variables.

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [March 30, 2017, 12:47pm UTC](https://discuss.elastic.co/t/how-to-pass-ruby-variable-to-logstash-field/80664/4 "2017-03-30T12:47:26Z")

</div>

What do you mean by "Logstash variables"? Perhaps you can give an example.

---

<div class="post-metadata">

**Author:** ![laptop\_data](https://avatars.discourse-cdn.com/v4/letter/l/3e96dc/32.png) [@laptop\_data](https://discuss.elastic.co/u/laptop_data)\
**Post date:** [March 30, 2017, 12:57pm UTC](https://discuss.elastic.co/t/how-to-pass-ruby-variable-to-logstash-field/80664/5 "2017-03-30T12:57:16Z")

</div>

The below is my sample code, where i'm passing input response to the filter. Here XYZ is a field name from the input.

> ruby{  
> "init =\> '@var'  
> code =\> "  
> event.set('var',event.get('XYZ'))  
> require 'open-uri'  
> response = open('[http://xyz.com?q="@var](http://xyz.com?q=%22@var)"').read  
> puts response  
> }

Here response will get some JSON data, now i should apply json filter on response.

```
like json {
           source => "response"
}

```

but it is not working.

---

<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:** [April 27, 2017, 12:57pm UTC](https://discuss.elastic.co/t/how-to-pass-ruby-variable-to-logstash-field/80664/6 "2017-04-27T12:57:22Z")

</div>

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