# How to convert a json field to string by using ruby filter?

**URL:** <https://discuss.elastic.co/t/how-to-convert-a-json-field-to-string-by-using-ruby-filter/46206>\
**Category:** Logstash\
**Created:** [April 4, 2016, 7:23am UTC](https://discuss.elastic.co/t/how-to-convert-a-json-field-to-string-by-using-ruby-filter/46206 "2016-04-04T07:23:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![abcfy2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abcfy2/32/453_2.png) [@abcfy2](https://discuss.elastic.co/u/abcfy2)\
**Post date:** [April 4, 2016, 7:23am UTC](https://discuss.elastic.co/t/how-to-convert-a-json-field-to-string-by-using-ruby-filter/46206/1 "2016-04-04T07:23:47Z")

</div>

Hi all. I have a event like:

```json
{"@timestamp":"2016-04-03T08:17:15.202Z","host":"shifudao","type":"rtds","loglevel":"WARN","classname":"hawkeyes.rtds.gate.protocol.frametype.SamplerReportData","logdetail":{"message":"设备00000068数据上报帧里的sid为空，可能未发过心跳，丢弃！","action":"fireEquipmentDataArrived(String writeHandlerID, String sid, Vertx vertx, List result)","sid":null,"params":{"writeHandlerID":"f7d2f035-b90f-4d2d-b9b4-d44a59f0227d","sid":null,"result":[{"eId":"00000068","date":1459672746202,"items":[[4,[26,17,0,0]],[5,[0,0,0,0]],[6,[-4,-1,0,0]],[7,[28,38,0,0]],[8,[-33,0,0,0]],[9,[-65,0,0,0]],[10,[0,0,0,0]],[11,[-52,0,0,0]],[12,[-65,0,0,0]],[32,[0,0,0,0]],[33,[0,0,0,0]],[35,[0,0,0,0]],[38,[-1,0,0,0]],[39,[0,0,0,0]],[40,[0,0,0,0]],[41,[0,0,0,0]],[42,[0,0,0,0]],[43,[0,0,0,0]],[44,[0,0,0,0]],[45,[0,0,0,0]],[48,[34,-55,0,0]],[49,[-36,-65,0,0]],[50,[-36,86,0,0]]]}]}},"@version":"1"}

```

And here is the `rubydebug` output:

```ruby
{
    "@timestamp" => 2016-04-03T08:17:15.202Z,
... SNIP ...
     "logdetail" => {
... SNIP ...
         "params" => {
            "writeHandlerID" => "f7d2f035-b90f-4d2d-b9b4-d44a59f0227d",
                       "sid" => nil,
                    "result" => [
                [0] {
                      "eId" => "00000068",
                     "date" => 1459672746202,
                    "items" => [
                        [0] [
                            [0] 4,
                            [1] [
                                [0] 26,
                                [1] 17,
                                [2] 0,
                                [3] 0
                            ]
                        ],
                        [1] [
... SNIP ...
      "@version" => "1"
}

```

Now I want to convert the `logdetail.params` filed to `string`, how to ?

I've tried:

```ruby
filter {
    ruby {
        code => "
            event['logdetail']['params'] = event['logdetail']['params'].to_s
        "
    }
}

```

But nothing happens.

---

<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:** [April 4, 2016, 7:26am UTC](https://discuss.elastic.co/t/how-to-convert-a-json-field-to-string-by-using-ruby-filter/46206/2 "2016-04-04T07:26:35Z")

</div>

Did you look at the [code of the json\_encode filter](https://github.com/logstash-plugins/logstash-filter-json_encode/blob/master/lib/logstash/filters/json_encode.rb) as I suggested earlier?

---

<div class="post-metadata">

**Author:** ![abcfy2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abcfy2/32/453_2.png) [@abcfy2](https://discuss.elastic.co/u/abcfy2)\
**Post date:** [April 4, 2016, 7:35am UTC](https://discuss.elastic.co/t/how-to-convert-a-json-field-to-string-by-using-ruby-filter/46206/3 "2016-04-04T07:35:31Z")

</div>

Yes, but for some reason I cannot install the plugin. So I want use built-in filter as far as possible.

I've look at the source code, but it's a little hard to me. And for me `hash.to_s` is more fit than `json.dump`, because some of this field is just string.

I try to copy the code like `event['logdetail']['params'] = event['logdetail']['params'].to_s`, but nothing happens.

---

<div class="post-metadata">

**Author:** ![abcfy2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abcfy2/32/453_2.png) [@abcfy2](https://discuss.elastic.co/u/abcfy2)\
**Post date:** [April 5, 2016, 7:31am UTC](https://discuss.elastic.co/t/how-to-convert-a-json-field-to-string-by-using-ruby-filter/46206/4 "2016-04-05T07:31:27Z")

</div>

I've been solved my issue.

```auto
    ruby {
        code => "
            field_to_be_converted = ['[logdetail][params][result]',
                                     '[logdetail][params][body]',
                                     '[logdetail][params][item]',
                                     '[logdetail][params][items]']
            for field in field_to_be_converted
                if event.include?(field) && event[field].class != String
                    event[field] = event[field].to_s
                end
            end
        "
    }

```

I'm not sure why this code could work.

---

<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:** [July 6, 2017, 5:03am UTC](https://discuss.elastic.co/t/how-to-convert-a-json-field-to-string-by-using-ruby-filter/46206/5 "2017-07-06T05:03:54Z")

</div>


