# Convert key value pair into hash

**URL:** <https://discuss.elastic.co/t/convert-key-value-pair-into-hash/260691>\
**Category:** Logstash\
**Created:** [January 11, 2021, 9:49am UTC](https://discuss.elastic.co/t/convert-key-value-pair-into-hash/260691 "2021-01-11T09:49:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![subash](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/subash/32/59912_2.png) [@subash](https://discuss.elastic.co/u/subash)\
**Post date:** [January 11, 2021, 9:49am UTC](https://discuss.elastic.co/t/convert-key-value-pair-into-hash/260691/1 "2021-01-11T09:49:31Z")

</div>

I have a field p1 with the value as partition=root,size=23.28GB,free=15.76GB

`p1 : partition=root,size=23.28GB,free=15.76GB`

I want to store this in an array field and the values in a hash, like below.

```
[partition][root] : {
                     size = 23.28GB,
                     free=15.76GB
                    }

```

Please help me to achieve this.

---

<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:** [January 11, 2021, 3:41pm UTC](https://discuss.elastic.co/t/convert-key-value-pair-into-hash/260691/2 "2021-01-11T15:41:22Z")

</div>

If you want the field name to be a variable you will have to use a ruby filter.

```
    dissect { mapping => { "p1" => "partition=%{[@metadata][partitionName]},size=%{[@metadata][size]},free=%{[@metadata][free]}" } }
    ruby {
        code => '
            h = { "size" => event.get("[@metadata][size]"), "free" => event.get("[@metadata][free]") }
            name = event.get("[@metadata][partitionName]")
            event.set("[partition][#{name}]", h)
        '
    }

```

Error handling is left as an exercise for the reader.

If you want to convert those GB to actual numbers then check out the [bytes](https://www.elastic.co/guide/en/logstash/current/plugins-filters-bytes.html) filter.

---

<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:** [February 8, 2021, 3:41pm UTC](https://discuss.elastic.co/t/convert-key-value-pair-into-hash/260691/3 "2021-02-08T15:41:35Z")

</div>

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