# Flatten json array to combine fields (right ruby code?))

**URL:** <https://discuss.elastic.co/t/flatten-json-array-to-combine-fields-right-ruby-code/103235>\
**Category:** Logstash\
**Created:** [October 9, 2017, 1:45pm UTC](https://discuss.elastic.co/t/flatten-json-array-to-combine-fields-right-ruby-code/103235 "2017-10-09T13:45:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![malhotras](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@malhotras](https://discuss.elastic.co/u/malhotras)\
**Post date:** [October 9, 2017, 1:45pm UTC](https://discuss.elastic.co/t/flatten-json-array-to-combine-fields-right-ruby-code/103235/1 "2017-10-09T13:45:42Z")

</div>

Here is how my input data looks like:

```
     {
     "details": [
      {
    "place": "abc",
    "group": 3,
     "year": 2006,
    "id": 1304,
      "street": "xyz 14",
       "lf_number": "0118",
     "code": 4433,
       "name": "abc coorperation",
    "group2": 3817"
    "group1": 32"
      "postal_code": "22926",
   "status": 2
     },
  {
   "place": "cbc",
      "group": 2,
     "year": 2007,
    "id": 4983,
    "street": "mnc 14",
     "lf_number": "0145",
    "code": 4433,
    "name": "abc coorperation", 
    "group2": 3817,
     "group1": 32"
    "postalcode": "22926",
      "status": 2
  }
  ],
    "p_name": "anacin",
    "@version": "1",
    "id": 28841
    }

```

I want to flatten this array more and combine various fields here. I am able to do that with one field but how can i use more than one field.

Here is what i have tried so far and it works for one field.

```
 filter{

            ruby {

     code => "if(!event.get('[details][0]').nil?)

         event.set('[status]',event.get('[details]').group_by { |m| {m['postal_code'],m['status']}}.join(','))
end"

 }

   }

```

This works only if i have one field. How can i group multiple fields here??

---

<div class="post-metadata">

**Author:** ![malhotras](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@malhotras](https://discuss.elastic.co/u/malhotras)\
**Post date:** [October 10, 2017, 1:54pm UTC](https://discuss.elastic.co/t/flatten-json-array-to-combine-fields-right-ruby-code/103235/2 "2017-10-10T13:54:12Z")

</div>

I am able to join only two fields now. But i want them to be seperated by @, when there is more than two hashes in the array.

filter{

```
            ruby {

```

code =\> "if(!event.get('[details][0]').nil?)

```
value = ''

```

value = event.get('[details]').map { |m| '{m.values\_at('id','name')}.join(',')' }.join('@')  
event.set('test',value)  
end"

}

}

This does not work as desired, please help to solve the problem.

---

<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:** [November 7, 2017, 1:54pm UTC](https://discuss.elastic.co/t/flatten-json-array-to-combine-fields-right-ruby-code/103235/3 "2017-11-07T13:54:49Z")

</div>

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