# Adding multiple nested json fields to an array field

**URL:** <https://discuss.elastic.co/t/adding-multiple-nested-json-fields-to-an-array-field/204279>\
**Category:** Logstash\
**Created:** [October 18, 2019, 9:49pm UTC](https://discuss.elastic.co/t/adding-multiple-nested-json-fields-to-an-array-field/204279 "2019-10-18T21:49:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kylemain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kylemain/32/48275_2.png) [@kylemain](https://discuss.elastic.co/u/kylemain)\
**Post date:** [October 18, 2019, 9:49pm UTC](https://discuss.elastic.co/t/adding-multiple-nested-json-fields-to-an-array-field/204279/1 "2019-10-18T21:49:23Z")

</div>

Hello,

I have a nested json field in my logs called "instanceId" :

"responseElements": { "instancesSet": { "items": [{ "instanceId": "i-02669ced00e8a3701", "currentState":{ "code": 32, "name": "shutting-down" }, "previousState": { "code": 16, "name": "running" } }, { "instanceId": "i-0050374c0e0a6306a", "currentState":{ "code": 32, "name": "shutting-down" }, "previousState": { "code": 16,"name": "running" } }] } }

As you can see there are two values for instanceId in this log (some logs will have many more). What I want to do is to extract all of the values of instanceId and put them into a new array field called "resp\_instance\_id".

This ruby code does not work as it doesn't create an array field:

```auto
ruby {
       code => '
         response_item_size = event.get("[responseElements][instancesSet][items]").length
          response_item_size.times do |index|
           event.set("[resp_instance_id][#{index}]", event.get("[responseElements][instancesSet] 
[items][#{index}][instanceId]"))
         end
        '
     }

```

This results in this strange looking field:

 ![49%20PM](https://us1.discourse-cdn.com/elastic/original/3X/9/f/9ff3f723a7a1ed19f7bea7dbf2dc1237b2590282.png)

Which in turn produces multiple fields in our platform like this (there are more than just two in this case):

 ![22%20PM](https://us1.discourse-cdn.com/elastic/original/3X/4/9/492a3553ef90094cf0ac8e7d21be8aa5a05e394d.png)

What I want is an _array_ field with the instead, like this:

"resp\_instance\_id" =\> [

[0] i-02669ced00e8a3701  
[1] i-0050374c0e0a6306a

]

I don't wan't to hardcode this by referencing the elements of instanceId manually, like this:

[responseElements][instancesSet][items][0][instanceId]  
[responseElements][instancesSet][items][1][instanceId]

Because instanceId will have an unknown number of values (not just two as in this example).  
I have tried using a loop in ruby to create a ruby array witch contains all of the elements of "instanceId" and was successful in creating that array. However, I was unable to do an add\_field using that array of values. It seems that add\_field can not be included in a loop in the ruby code, but rather must come after the closing ' of the ruby code block. I do know if you use "add\_field" to create the same field multiple times with different values that the resulting field will be an array field. But, without being able to use "add\_field" inside of the ruby loop, this does not seem possible.

---

<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:** [October 19, 2019, 1:06pm UTC](https://discuss.elastic.co/t/adding-multiple-nested-json-fields-to-an-array-field/204279/2 "2019-10-19T13:06:10Z")

</div>

You can do it using

```
        code => '
            response_items = event.get("[responseElements][instancesSet][items]")
            a = []
            response_items.each_index do |x|
                a << response_items[x]["instanceId"]
            end
            event.set("resp_instance_id", a)
        '
    }
```

---

<div class="post-metadata">

**Author:** ![kylemain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kylemain/32/48275_2.png) [@kylemain](https://discuss.elastic.co/u/kylemain)\
**Post date:** [October 22, 2019, 7:29pm UTC](https://discuss.elastic.co/t/adding-multiple-nested-json-fields-to-an-array-field/204279/3 "2019-10-22T19:29:17Z")

</div>

worked! thank you.

---

<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 19, 2019, 7:29pm UTC](https://discuss.elastic.co/t/adding-multiple-nested-json-fields-to-an-array-field/204279/4 "2019-11-19T19:29:17Z")

</div>

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