# Logstash move subfield to top field

**URL:** <https://discuss.elastic.co/t/logstash-move-subfield-to-top-field/244309>\
**Category:** Logstash\
**Created:** [August 9, 2020, 3:59pm UTC](https://discuss.elastic.co/t/logstash-move-subfield-to-top-field/244309 "2020-08-09T15:59:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![gbasic](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@gbasic](https://discuss.elastic.co/u/gbasic)\
**Post date:** [August 9, 2020, 3:59pm UTC](https://discuss.elastic.co/t/logstash-move-subfield-to-top-field/244309/1 "2020-08-09T15:59:02Z")

</div>

```auto
 "_score" : 1.0,
        "_source" : {
          "Europe" : {
            "DateTime" : {
              "Date" : "08.08.2020",
              "Time" : "15"
            },
            "City" : [
              {
                "Data" : {
                  "Temp" : "32",
                  "Humidity" : "33",
                  "WindDirection" : "NE",
                  "WindSpeed" : "03"

                },
                "CityName" : "Amsterdam"
              },

```

Hi, I am a complete beginner and I started playing around with the logstash conf file. I was wondering what is the best way to move fields (Temp,Humidity,WindDirection,WindSpeed) to the same level as "CityName" while removing the "Data" object.

---

<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 9, 2020, 4:03pm UTC](https://discuss.elastic.co/t/logstash-move-subfield-to-top-field/244309/2 "2020-08-09T16:03:51Z")

</div>

Something very similar to [this](https://discuss.elastic.co/t/how-to-dynamically-move-nested-key-value-to-root-level/180006/2), except that instead of "doc" it would be "[City][Data]" and instead of using just k in the event.set it would be "[City][#{k}]".

---

<div class="post-metadata">

**Author:** ![gbasic](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@gbasic](https://discuss.elastic.co/u/gbasic)\
**Post date:** [August 9, 2020, 4:23pm UTC](https://discuss.elastic.co/t/logstash-move-subfield-to-top-field/244309/3 "2020-08-09T16:23:16Z")

</div>

Thanks for the answer I tried it but I got an error:  
`Ruby exception occurred: undefined method 'each' for nil:NilClass`

I assume this is something I need to account for, do you have a suggestion on how to do that? I am not really familiar with ruby.

---

<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 9, 2020, 4:40pm UTC](https://discuss.elastic.co/t/logstash-move-subfield-to-top-field/244309/4 "2020-08-09T16:40:54Z")

</div>

> [@gbasic](#):
>
> Thanks for the answer I tried it but I got an error:  
> `Ruby exception occurred: undefined method 'each' for nil:NilClass`

That is telling you that you called event.get on a field that does not exist. Try "[Europe][City][Data]" and "[Europe][City][#{k}]".

---

<div class="post-metadata">

**Author:** ![gbasic](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@gbasic](https://discuss.elastic.co/u/gbasic)\
**Post date:** [August 9, 2020, 4:42pm UTC](https://discuss.elastic.co/t/logstash-move-subfield-to-top-field/244309/5 "2020-08-09T16:42:05Z")

</div>

```auto
"Europe" => {
        "DateTime" => {
            "Time" => "15",
             "Date" => "08.08.2020"
        },
               "City" => [
            [0] {
                "Data" => {
                           "Humidity" => "33",
                     "WindDirection" => "NE",
                            "Temp" => "32",
                    "WindSpeed" => "03"
                },
                "CityName" => "Amsterdam"
            },
            [1] {
                "Data" => {
                           "Humidity" => "18",
                     "WindDirection" => "E",
                            "Temp" => "33",
                    "WindSpeed" => "06"
                },
                "CityName" => "Ankara"
            },

```

This is the output when I run the config file as you can see I have multiple cities could that be the issue?

---

<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 9, 2020, 6:02pm UTC](https://discuss.elastic.co/t/logstash-move-subfield-to-top-field/244309/6 "2020-08-09T18:02:30Z")

</div>

> [@gbasic](#):
>
> could that be the issue?

Yes indeed! You can try this, which I have not tested.

```
ruby {
    code => '
        oldCities = event.get("[Europe][City]")
        cities = []
        oldCities.each { |x|
            x["Data"].each { | k, v |
                x[k] = v
            }
           x.delete("Data")
           cities << x
        }
        event.set([Europe][City]", cities)
    '
}

```

---

<div class="post-metadata">

**Author:** ![gbasic](https://avatars.discourse-cdn.com/v4/letter/g/9de0a6/32.png) [@gbasic](https://discuss.elastic.co/u/gbasic)\
**Post date:** [August 10, 2020, 11:52am UTC](https://discuss.elastic.co/t/logstash-move-subfield-to-top-field/244309/7 "2020-08-10T11:52:09Z")

</div>

It works thank you so much 🙂

---

<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 7, 2020, 11:52am UTC](https://discuss.elastic.co/t/logstash-move-subfield-to-top-field/244309/8 "2020-09-07T11:52:25Z")

</div>

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