# How to iterate through several elements and create a new field with the content of each element

**URL:** <https://discuss.elastic.co/t/how-to-iterate-through-several-elements-and-create-a-new-field-with-the-content-of-each-element/215656>\
**Category:** Logstash\
**Created:** [January 20, 2020, 12:10am UTC](https://discuss.elastic.co/t/how-to-iterate-through-several-elements-and-create-a-new-field-with-the-content-of-each-element/215656 "2020-01-20T00:10:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alejovaz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alejovaz/32/61164_2.png) [@alejovaz](https://discuss.elastic.co/u/alejovaz)\
**Post date:** [January 20, 2020, 12:10am UTC](https://discuss.elastic.co/t/how-to-iterate-through-several-elements-and-create-a-new-field-with-the-content-of-each-element/215656/1 "2020-01-20T00:10:13Z")

</div>

My Logstash receive of a http input this field

PANELS.memory {  
"availableBytes": 6212550656,  
"totalBytes": 8493137920,  
"type": "physical"  
},  
{  
"availableBytes": 14702407680,  
"totalBytes": 17256087552,  
"type": "virtual"  
}

If I split this field, it will save 2 (or more) different elasticsearch documents, but I need to save 1 only elasticsearch document of the form:

PANELS.memory0 {  
"availableBytes": 6212550656,  
"totalBytes": 8493137920,  
"type": "physical"  
},  
PANELS.memory1 {  
"availableBytes": 14702407680,  
"totalBytes": 17256087552,  
"type": "virtual"  
}  
PANELS.memoryN {  
"availableBytes": 14702407680,  
"totalBytes": 17256087552,  
"type": "cache"  
}

I tried to create a ruby filter to iterate on "n" elements (if applicable) but I don't know enough ruby to solve the problem.

I really appreciate your support

In the label JSON, the field looks like this:

```
"PANELS": {
      "memory": [
    {
      "availableBytes": 6211796992,
      "type": "physical",
      "totalBytes": 8493137920
    },
    {
      "availableBytes": 14701268992,
      "type": "virtual",
      "totalBytes": 17256087552
    },
    {
      "availableBytes": 6211796992,
      "type": "cache",
      "totalBytes": 8493137920
    }
  ],

```

}

---

<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:** [January 20, 2020, 3:25pm UTC](https://discuss.elastic.co/t/how-to-iterate-through-several-elements-and-create-a-new-field-with-the-content-of-each-element/215656/2 "2020-01-20T15:25:38Z")

</div>

You could try something similar to [this](https://discuss.elastic.co/t/parsing-an-array-in-ruby/121254/2).

---

<div class="post-metadata">

**Author:** ![alejovaz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alejovaz/32/61164_2.png) [@alejovaz](https://discuss.elastic.co/u/alejovaz)\
**Post date:** [January 21, 2020, 12:09pm UTC](https://discuss.elastic.co/t/how-to-iterate-through-several-elements-and-create-a-new-field-with-the-content-of-each-element/215656/3 "2020-01-21T12:09:25Z")

</div>

Thanks @Badger, I used [this code](https://discuss.elastic.co/t/set-fields-into-array-solved/73995/3) of @adrianolimit and solve the problem.

The ruby filter is a powerfull and needed tool.

Thanks.

---

<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:** [February 18, 2020, 12:09pm UTC](https://discuss.elastic.co/t/how-to-iterate-through-several-elements-and-create-a-new-field-with-the-content-of-each-element/215656/4 "2020-02-18T12:09:29Z")

</div>

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