# How to convert a nested field type?

**URL:** https://discuss.elastic.co/t/how-to-convert-a-nested-field-type/46025
**Category:** Logstash
**Created:** [April 1, 2016, 6:22am UTC](https://discuss.elastic.co/t/how-to-convert-a-nested-field-type/46025 "2016-04-01T06:22:42Z")
**Posts on this page:** 13
**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 1, 2016, 6:22am UTC](https://discuss.elastic.co/t/how-to-convert-a-nested-field-type/46025/1 "2016-04-01T06:22:42Z")

</div>

I find `mutate` filter can convert a field type, but how to convert a nested one?

I've tried:

```auto
mutate {
    convert => {"logdetail.params" => "string"}
}

```

And

```auto
mutate {
    convert => {"[logdetail][params]" => "string"}
}

```

But failed.

---

<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 1, 2016, 6:24am UTC](https://discuss.elastic.co/t/how-to-convert-a-nested-field-type/46025/2 "2016-04-01T06:24:57Z")

</div>

The latter should work. What does the message look like? Please use a `stdout { codec => rubydebug }` output so we get the raw data.

---

<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 1, 2016, 6:26am UTC](https://discuss.elastic.co/t/how-to-convert-a-nested-field-type/46025/3 "2016-04-01T06:26:34Z")

</div>

```auto
{
    "@timestamp" => 2016-04-01T06:17:16.494Z,
          "host" => "shifudao",
          "type" => "rtds",
      "loglevel" => "WARN",
     "classname" => "hawkeyes.rtds.gate.protocol.frametype.SamplerReportData",
     "logdetail" => {
        "message" => "设备00004650数据上报帧里的sid为空，可能未发过心跳，丢弃！",
         "action" => "fireEquipmentDataArrived(String writeHandlerID, String sid, Vertx vertx, List result)",
            "sid" => nil,
         "params" => {
            "writeHandlerID" => "b6728301-653e-488c-91b8-fc2aa52c1312",
                       "sid" => nil,
                    "result" => [
                [0] {
                      "eId" => "00004650",
                     "date" => 963328184494,
                    "items" => [
                        [0] [
                            [0] 1,
                            [1] [
                                [0] 74,
                                [1] 0,
                                [2] 0,
                                [3] 0
                            ]
                        ],
                        [1] [
                            [0] 2,
                            [1] [
                                [0] -119,
                                [1] 0,
                                [2] 0,
                                [3] 0
                            ]
                        ],
                        [2] [
                            [0] 3,
                            [1] [
                                [0] 12,
                                [1] 0,
                                [2] 0,
                                [3] 0
                            ]
                        ],
                        [3] [
                            [0] 4,
                            [1] [
                                [0] 12,
                                [1] 0,
                                [2] 0,
                                [3] 0
                            ]
                        ],
                        [4] [
                            [0] 5,
                            [1] [
                                [0] 110,
                                [1] 5,
                                [2] 0,
                                [3] 0
                            ]
                        ],
                        [5] [
                            [0] 6,
                            [1] [
                                [0] 61,
                                [1] 5,
                                [2] 0,
                                [3] 0
                            ]
                        ],
                        [6] [
                            [0] 7,
                            [1] [
                                [0] -18,
                                [1] 5,
                                [2] 0,
                                [3] 0
                            ]
                        ],
                        [7] [
                            [0] 8,
                            [1] [
                                [0] 3,
                                [1] 8,
                                [2] 0,
                                [3] 0
                            ]
                        ],
                        [8] [
                            [0] 9,
                            [1] [
                                [0] 0,
                                [1] 0,
                                [2] 0,
                                [3] 0
                            ]
                        ]
                    ]
                }
            ]
        }
    },
      "@version" => "1"
}

```

`logdetail.params` is a nested json object, and I want to convert it to string.

---

<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 1, 2016, 7:32am UTC](https://discuss.elastic.co/t/how-to-convert-a-nested-field-type/46025/4 "2016-04-01T07:32:13Z")

</div>

Oh. Such objects can't be converted to a string (see below). Perhaps the [json\_encode filter](https://www.elastic.co/guide/en/logstash/current/plugins-filters-json_encode.html) would be useful?

> <https://github.com/logstash-plugins/logstash-filter-mutate/blob/v2.0.6/lib/logstash/filters/mutate.rb#L271-L280>

---

<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 1, 2016, 7:32am UTC](https://discuss.elastic.co/t/how-to-convert-a-nested-field-type/46025/5 "2016-04-01T07:32:58Z")

</div>

Oh, I read the official doc carefully, and I find this reason:

```auto
 If the field is a hash, no action will be taken.

```

So this field is a hash. But how to I perform a `toString()` action?

---

<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 1, 2016, 12:40pm UTC](https://discuss.elastic.co/t/how-to-convert-a-nested-field-type/46025/6 "2016-04-01T12:40:01Z")

</div>

But `json_encode` convert a string to json. I want to convert a nested json to string. How to?

---

<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 1, 2016, 1:47pm UTC](https://discuss.elastic.co/t/how-to-convert-a-nested-field-type/46025/7 "2016-04-01T13:47:24Z")

</div>

No, you have a field that happens to be nested that you for some reason want to transform to a string representation. The json\_encode filter serializes a field to JSON. JSON is a string.

---

<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 1, 2016, 2:24pm UTC](https://discuss.elastic.co/t/how-to-convert-a-nested-field-type/46025/8 "2016-04-01T14:24:45Z")

</div>

Great! `json_encode` works for me now.

And is there any built-in filter alternative ? Maybe `ruby` could also work, but I'm not good at not ruby. I don't know how to use `ruby` to implement.

---

<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 2, 2016, 2:35pm UTC](https://discuss.elastic.co/t/how-to-convert-a-nested-field-type/46025/9 "2016-04-02T14:35:40Z")

</div>

> And is there any built-in filter alternative ?

Alternative that does what?

---

<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 2, 2016, 5:53pm UTC](https://discuss.elastic.co/t/how-to-convert-a-nested-field-type/46025/10 "2016-04-02T17:53:20Z")

</div>

Do the same with json\_encode, just like my title. But it's a built-in filter. Maybe the ruby filter could do the same thing, but I'm not good at ruby, is there any examples?

---

<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 2, 2016, 5:57pm UTC](https://discuss.elastic.co/t/how-to-convert-a-nested-field-type/46025/11 "2016-04-02T17:57:07Z")

</div>

So what you're really asking for is JSON serialization with a core plugin? That should indeed be possible with a ruby filter but I don't have any example at hand, except of course the json\_encode source code.

---

<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 3, 2016, 2:42am UTC](https://discuss.elastic.co/t/how-to-convert-a-nested-field-type/46025/12 "2016-04-03T02:42:01Z")

</div>

Thanks all the same.

---

<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:04am UTC](https://discuss.elastic.co/t/how-to-convert-a-nested-field-type/46025/13 "2017-07-06T05:04:12Z")

</div>


