# How can I flatten an array in my document

**URL:** <https://discuss.elastic.co/t/how-can-i-flatten-an-array-in-my-document/235807>\
**Category:** Logstash\
**Created:** [June 4, 2020, 4:10pm UTC](https://discuss.elastic.co/t/how-can-i-flatten-an-array-in-my-document/235807 "2020-06-04T16:10:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![genehunter29009](https://avatars.discourse-cdn.com/v4/letter/g/45deac/32.png) [@genehunter29009](https://discuss.elastic.co/u/genehunter29009)\
**Post date:** [June 4, 2020, 4:10pm UTC](https://discuss.elastic.co/t/how-can-i-flatten-an-array-in-my-document/235807/1 "2020-06-04T16:10:06Z")

</div>

I am attempting to flatten an array either in logstash or ingestion processor.

"clientType" : "Desktop Browser",  
"stringProperties" : [  
{  
"key" : "usertype",  
"applicationId" : "Client",  
"internalApplicationId" : "APPLICATION-89",  
"value" : "R"  
},  
{  
"key" : "firmid",  
"applicationId" : "Client",  
"internalApplicationId" : "APPLICATION-89",  
"value" : "1"  
},  
{  
"key" : "firmname",  
"applicationId" : "Client",  
"internalApplicationId" : "APPLICATION-89",  
"value" : "ABC"  
}

any help would be appreciated. I am having trouble figuring the best way to do this, ultimately I would like it to look like this

"clientType" : "Desktop Browser",  
"stringProperty.usertype" : "R",

---

<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:** [June 4, 2020, 4:21pm UTC](https://discuss.elastic.co/t/how-can-i-flatten-an-array-in-my-document/235807/3 "2020-06-04T16:21:08Z")

</div>

Use a ruby filter. I have not tested it but something like this...

```
ruby {
    code => '
        props = event.get("stringProperties")
        if props
            props.each { |x|
                key = x["key"]
                event.set("stringProperties.#{key}", x["value"])
            }
            event.remove("stringProperties")
        end
    }
}
```

---

<div class="post-metadata">

**Author:** ![genehunter29009](https://avatars.discourse-cdn.com/v4/letter/g/45deac/32.png) [@genehunter29009](https://discuss.elastic.co/u/genehunter29009)\
**Post date:** [June 4, 2020, 4:24pm UTC](https://discuss.elastic.co/t/how-can-i-flatten-an-array-in-my-document/235807/4 "2020-06-04T16:24:41Z")

</div>

> [@Badger](#):
>
> ```auto
> ruby {
> code => '
> props = event.get("stringProperties")
> if props
> props.each { |x|
> key = x["key"]
> event.set("stringProperties.#{key}", x["value"])
> }
> event.remove("stringProperties")
> end
> }
> }
> 
> ```

thank you, will try it out and let you know.

---

<div class="post-metadata">

**Author:** ![genehunter29009](https://avatars.discourse-cdn.com/v4/letter/g/45deac/32.png) [@genehunter29009](https://discuss.elastic.co/u/genehunter29009)\
**Post date:** [June 4, 2020, 4:42pm UTC](https://discuss.elastic.co/t/how-can-i-flatten-an-array-in-my-document/235807/5 "2020-06-04T16:42:14Z")

</div>

> [@Badger](#):
>
> `code => '`

I have never used ruby but I am getting an error does the string have an end?

code =\> 'start of string, but i see no end '

---

<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:** [June 4, 2020, 5:00pm UTC](https://discuss.elastic.co/t/how-can-i-flatten-an-array-in-my-document/235807/6 "2020-06-04T17:00:59Z")

</div>

> [@Badger](#):
>
> ```auto
> end
> }
> }
> 
> ```

That should be

```
        end
    '
}

```

---

<div class="post-metadata">

**Author:** ![genehunter29009](https://avatars.discourse-cdn.com/v4/letter/g/45deac/32.png) [@genehunter29009](https://discuss.elastic.co/u/genehunter29009)\
**Post date:** [June 5, 2020, 8:58am UTC](https://discuss.elastic.co/t/how-can-i-flatten-an-array-in-my-document/235807/7 "2020-06-05T08:58:15Z")

</div>

That worked perfectly thank you very much.

---

<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:** [July 3, 2020, 8:58am UTC](https://discuss.elastic.co/t/how-can-i-flatten-an-array-in-my-document/235807/8 "2020-07-03T08:58:21Z")

</div>

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