# Problem converting multi-delimited kv string

**URL:** <https://discuss.elastic.co/t/problem-converting-multi-delimited-kv-string/226527>\
**Category:** Logstash\
**Created:** [April 4, 2020, 5:28pm UTC](https://discuss.elastic.co/t/problem-converting-multi-delimited-kv-string/226527 "2020-04-04T17:28:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![adude946](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@adude946](https://discuss.elastic.co/u/adude946)\
**Post date:** [April 4, 2020, 5:28pm UTC](https://discuss.elastic.co/t/problem-converting-multi-delimited-kv-string/226527/1 "2020-04-04T17:28:11Z")

</div>

I'm lost with converting 1 mysql column to ES via logstash. I can convert a single kv to the expected format but not multiple.

Data is in 1 mysql column called " **profile**". It is going to ES under same field name, mapping type = object.

1. Keys are open ended, may be different for each record, but schema is same
2. "=" is defining the key
3. Everything within [..] are array values for the preceding key
4. The ";" outside the [...] is indicating end of that value set for the preceding key
5. The ";" inside the [...] is indicating end of that value

## Sample input data in mysql column: profile

record1:  
`hobbies=[football;basketball;hockey;];interests=[reading;dancing;];food=[burger;pizza;ice-cream;];`

record2:  
`medical=[thyroid;anemia;];food=[chicken;];`

## Expected result in ES:

record1:

```
"profile":{
  "hobbies":	["football","basketball","hockey"],
  "interests":	["reading","dancing"],
  "food":	["burger","pizza","ice-cream"]
}

```

record2:

```
"profile": {
  "medical":	["thyroid","anemia"],
  "food": ["chicken"]
}
```

---

<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:** [April 4, 2020, 6:01pm UTC](https://discuss.elastic.co/t/problem-converting-multi-delimited-kv-string/226527/2 "2020-04-04T18:01:08Z")

</div>

Does the include\_brackets option not help with that?

---

<div class="post-metadata">

**Author:** ![adude946](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@adude946](https://discuss.elastic.co/u/adude946)\
**Post date:** [April 4, 2020, 8:01pm UTC](https://discuss.elastic.co/t/problem-converting-multi-delimited-kv-string/226527/3 "2020-04-04T20:01:50Z")

</div>

Only partly. I separated the fields using:

```
  kv {
    source => "profile"
    include_brackets => false
    field_split_pattern => "\];"
    remove_char_value => "\[\]"
    target => "profile2"
  }

```

This outputs:

```
   "profile2": {
    "field1" : "football;basketball;hockey;",
    "field2" : "reading;dancing;"
}

```

But now for each field inside profile2 have to convert them to an array. These fields are open ended per record, upto n number of fields, so can't hardcode the names. I'm assuming I'll need to run a loop for whatever random fields exist inside profile2 do a split by ; to make them each an array.

It should be:

```
   "profile2": {
    "field1" : ["football","basketball","hockey"],
    "field2" : ["reading","dancing"]
}
```

---

<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:** [April 4, 2020, 10:46pm UTC](https://discuss.elastic.co/t/problem-converting-multi-delimited-kv-string/226527/4 "2020-04-04T22:46:13Z")

</div>

Actually I was suggesting

```
include_brackets => true
field_split_pattern => ";"
```

---

<div class="post-metadata">

**Author:** ![adude946](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@adude946](https://discuss.elastic.co/u/adude946)\
**Post date:** [April 4, 2020, 11:11pm UTC](https://discuss.elastic.co/t/problem-converting-multi-delimited-kv-string/226527/5 "2020-04-04T23:11:28Z")

</div>

That produces the same output:

```auto
   "profile2": {
    "field1" : "football;basketball;hockey;",
    "field2" : "reading;dancing;"
}

```

But now each value has to become an array for these random fields.

---

<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:** [April 5, 2020, 1:17pm UTC](https://discuss.elastic.co/t/problem-converting-multi-delimited-kv-string/226527/6 "2020-04-05T13:17:20Z")

</div>

You can use a ruby filter to do the splits. I haven't tested it, but something like

```
ruby {
    code => '
        event.get("profile2").each { |k, v|
            event.remove("[profile2][#{k}]") # Not sure if this is needed
            event.set("[profile2][#{k}]", v.split(";"))
        }
    '
}
```

---

<div class="post-metadata">

**Author:** ![adude946](https://avatars.discourse-cdn.com/v4/letter/a/3da27b/32.png) [@adude946](https://discuss.elastic.co/u/adude946)\
**Post date:** [April 5, 2020, 7:19pm UTC](https://discuss.elastic.co/t/problem-converting-multi-delimited-kv-string/226527/7 "2020-04-05T19:19:36Z")

</div>

The ruby filter works perfect. Second line not needed. Thanks a lot.

---

<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:** [May 3, 2020, 7:19pm UTC](https://discuss.elastic.co/t/problem-converting-multi-delimited-kv-string/226527/8 "2020-05-03T19:19:40Z")

</div>

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