# Mutate Value to Keyname

**URL:** <https://discuss.elastic.co/t/mutate-value-to-keyname/168184>\
**Category:** Logstash\
**Created:** [February 13, 2019, 10:33am UTC](https://discuss.elastic.co/t/mutate-value-to-keyname/168184 "2019-02-13T10:33:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rebort](https://avatars.discourse-cdn.com/v4/letter/r/d07c76/32.png) [@rebort](https://discuss.elastic.co/u/rebort)\
**Post date:** [February 13, 2019, 10:33am UTC](https://discuss.elastic.co/t/mutate-value-to-keyname/168184/1 "2019-02-13T10:33:11Z")

</div>

Hi, i have a Problem and i need a hint.

I get this keys:  
{  
"deviceCustomString1": "segmentA",  
"deviceCustomString1Label": " Segment",  
"deviceCustomString2": "c833-cb3804056",  
"deviceCustomString2Label": "Policy UUID"  
}

I need to mutate the Value from deviceCustomStringXLabel as a Key for the Value in deviceCustomStringX

until now i see in debug that the Number goes up to 8. So i search for an generic way to do this mutation. Any Hints?

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:** [February 13, 2019, 11:29am UTC](https://discuss.elastic.co/t/mutate-value-to-keyname/168184/2 "2019-02-13T11:29:42Z")

</div>

Try something like

```
    ruby {
        code => '
            event.to_hash.each { |k, v|
                if k.match(/Label$/)
                    newk = k.gsub(/(.*)Label$/, "\\1")
                    newv = event.get(newk)
                    if newv
                        event.set(v, newv)
                        event.remove(k)
                        event.remove(newk)
                    end
                end
            }
        '
    }

```

If you really have a space in " Segment" you might want to add code to fix that.

---

<div class="post-metadata">

**Author:** ![rebort](https://avatars.discourse-cdn.com/v4/letter/r/d07c76/32.png) [@rebort](https://discuss.elastic.co/u/rebort)\
**Post date:** [February 13, 2019, 1:04pm UTC](https://discuss.elastic.co/t/mutate-value-to-keyname/168184/3 "2019-02-13T13:04:35Z")

</div>

Thank you very much, this worked for me.

the space in " Segment" was my writing error

---

<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:** [March 13, 2019, 1:09pm UTC](https://discuss.elastic.co/t/mutate-value-to-keyname/168184/4 "2019-03-13T13:09:16Z")

</div>

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