# Parsing array of elements

**URL:** <https://discuss.elastic.co/t/parsing-array-of-elements/300462>\
**Category:** Logstash\
**Created:** [March 23, 2022, 1:44pm UTC](https://discuss.elastic.co/t/parsing-array-of-elements/300462 "2022-03-23T13:44:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eliz](https://avatars.discourse-cdn.com/v4/letter/e/848f3c/32.png) [@eliz](https://discuss.elastic.co/u/eliz)\
**Post date:** [March 23, 2022, 1:44pm UTC](https://discuss.elastic.co/t/parsing-array-of-elements/300462/1 "2022-03-23T13:44:50Z")

</div>

Hello, I am looking for a way to go from this message

```auto
{
  "parent_field": [
    "Key 1:red",
    "Key 1:blue",
    "Key 2:this is green"
  ]
}

```

to:

```auto
{
    "key#1": ["red", "blue"],
    "key#2": "this is green"
}

```

Tried with gsub to replace the space with hashtag and then kv (with transform\_key =\> "lowercase") and worked, but the gsub also replaces the spaces of the values as well and last key has "this#is#green" as value.

Thanks!

---

<div class="post-metadata">

**Author:** ![aaron-nimocks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/aaron-nimocks/32/73965_2.png) [@aaron-nimocks](https://discuss.elastic.co/u/aaron-nimocks)\
**Post date:** [March 23, 2022, 2:44pm UTC](https://discuss.elastic.co/t/parsing-array-of-elements/300462/2 "2022-03-23T14:44:39Z")

</div>

Think this will get you close. Not sure the requirements from removing `this is` from `this is green` but I think you have that part figured out where you can add it.

**Conf**

```auto
input {
  generator {
    lines => ['{ "parent_field": [ "Key 1:red", "Key 1:blue", "Key 2:this is green"] }' ]
    codec => json
    count => 1
  }
}
filter {
  kv { 
    source => "parent_field"
    value_split => ":"
    field_split => ","
  }
  mutate { rename => { "Key 1" => "key#1" } }
  mutate { rename => { "Key 2" => "key#2" } }
}
output {
  stdout { codec => json }
}

```

**Output**

```auto
{
  "key#1": [
    "red",
    "blue"
  ],
  "key#2": "this is green"
}

```

---

<div class="post-metadata">

**Author:** ![eliz](https://avatars.discourse-cdn.com/v4/letter/e/848f3c/32.png) [@eliz](https://discuss.elastic.co/u/eliz)\
**Post date:** [March 23, 2022, 2:54pm UTC](https://discuss.elastic.co/t/parsing-array-of-elements/300462/3 "2022-03-23T14:54:18Z")

</div>

Thanks, Aaron. Appreciate you help. I forgot to mention that the fields in the array are dynamic and I do not know their names. I won't be able to rename them as you suggested.

---

<div class="post-metadata">

**Author:** ![eliz](https://avatars.discourse-cdn.com/v4/letter/e/848f3c/32.png) [@eliz](https://discuss.elastic.co/u/eliz)\
**Post date:** [March 23, 2022, 3:01pm UTC](https://discuss.elastic.co/t/parsing-array-of-elements/300462/4 "2022-03-23T15:01:01Z")

</div>

I mean, I only know the name of the parent field (parent key) but the number of the fields in the array/list is variable and their names are almost imposible to predict. The only thing I know is that I need to replace their spaces if present.

---

<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:** [April 20, 2022, 3:01pm UTC](https://discuss.elastic.co/t/parsing-array-of-elements/300462/5 "2022-04-20T15:01:17Z")

</div>

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