# Convert object into nested array using Ruby Logstash

**URL:** <https://discuss.elastic.co/t/convert-object-into-nested-array-using-ruby-logstash/210160>\
**Category:** Logstash\
**Created:** [December 2, 2019, 10:42am UTC](https://discuss.elastic.co/t/convert-object-into-nested-array-using-ruby-logstash/210160 "2019-12-02T10:42:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Reshmi](https://avatars.discourse-cdn.com/v4/letter/r/87869e/32.png) [@Reshmi](https://discuss.elastic.co/u/Reshmi)\
**Post date:** [December 2, 2019, 10:42am UTC](https://discuss.elastic.co/t/convert-object-into-nested-array-using-ruby-logstash/210160/1 "2019-12-02T10:42:41Z")

</div>

My XML structure is

> ```
> <OrderReleases>                 
> <OrderRelease ShipNode="53" />                
> </OrderReleases>
> 
> ```

After xml parsing with the below code

> xml{  
> source =\> "message"  
> store\_xml =\> true  
> target =\> "order"  
> force\_array =\> false  
> }

Getting it as an object instead of list

```
"OrderReleases": {
            "ShipNode": "53"
  }

```

But I would like to have the output structure like below

```
 "OrderReleases": [
     {
                "ShipNode": "53"
      }
  ]

```

How can I achieve it using ruby?

Please advise any one.

---

<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:** [December 2, 2019, 3:27pm UTC](https://discuss.elastic.co/t/convert-object-into-nested-array-using-ruby-logstash/210160/2 "2019-12-02T15:27:02Z")

</div>

Remove this

```
force_array => false

```

and it will be an array, as you want.

---

<div class="post-metadata">

**Author:** ![Reshmi](https://avatars.discourse-cdn.com/v4/letter/r/87869e/32.png) [@Reshmi](https://discuss.elastic.co/u/Reshmi)\
**Post date:** [December 3, 2019, 5:41am UTC](https://discuss.elastic.co/t/convert-object-into-nested-array-using-ruby-logstash/210160/3 "2019-12-03T05:41:40Z")

</div>

Thanks Badger for the reply. But can't use your solution as my XML is pretty huge and there are other nested arrays and objects into it for which I need force\_array =\> false.

I am looking for some ruby code to handle this.

Would like to add one more input to the question when the above XML OrderReleases contains more than one element (OrderRelease) then the data is storing in the correct format into the Elastic but the same is not the case if it has only one element.

Tried something like this:

```
messageOrderRelease= []
event.get("[OrderReleases][OrderRelease]").each { |item|
messageOrderRelease << item
logger.info("item value", "value" => item)
}

```

Here item is coming as null. And logger.info is writing ["Shipnode": "53"]

---

<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:** [December 31, 2019, 5:41am UTC](https://discuss.elastic.co/t/convert-object-into-nested-array-using-ruby-logstash/210160/4 "2019-12-31T05:41:41Z")

</div>

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