# Working with key:value arrays

**URL:** <https://discuss.elastic.co/t/working-with-key-value-arrays/283772>\
**Category:** Logstash\
**Created:** [September 9, 2021, 12:34pm UTC](https://discuss.elastic.co/t/working-with-key-value-arrays/283772 "2021-09-09T12:34:32Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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:** [September 9, 2021, 9:37pm UTC](https://discuss.elastic.co/t/working-with-key-value-arrays/283772/4 "2021-09-09T21:37:31Z")

</div>

Your values contain commas, so a simple mutate+split will incorrectly parse the [message]. Luckily, a kv filter parses it correctly

```
kv { field_split => "," value_split => ":" trim_key => '"' target => "[@metadata][kvData]" }
    ruby {
        code => '
            kvData = event.get("[@metadata][kvData]")
            if kvData
                data = []
                kvData.each { |k, v|
                    data << { "Field1" => k, "Field2" => v }
                }
                event.set("someField", data)
            end
        '
    }
    split { field => "someField" }

```

You can move the results to the top level using [this](https://discuss.elastic.co/t/how-to-dynamically-move-nested-key-value-to-root-level/180006/2) code.

I think using a kv filter is fragile, and I would do it in ruby.

```
    ruby {
        code => '
            matches = event.get("message").scan(/"([^"]*)":"([^"]*)"(,|$)/)
            data = []
            matches.each_index { |x|
                data << { "Field1" => matches[x][0], "Field2" => matches[x][1] }
            }
            event.set("someField", data)
        '
    }
    split { field => "someField" }

```

Although that regexp may not be any less fragile than the kv filter 😃

---

_[View the full topic](https://discuss.elastic.co/t/working-with-key-value-arrays/283772)._
