# Ruby Iteration in Logstash help

**URL:** https://discuss.elastic.co/t/ruby-iteration-in-logstash-help/224054
**Category:** Logstash
**Created:** [March 18, 2020, 9:05am UTC](https://discuss.elastic.co/t/ruby-iteration-in-logstash-help/224054 "2020-03-18T09:05:32Z")
**Posts on this page:** 1
**Showing post:** 4

<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: [March 22, 2020, 6:56pm UTC](https://discuss.elastic.co/t/ruby-iteration-in-logstash-help/224054/4 "2020-03-22T18:56:19Z")

</div>

OK, I am unclear on what output format you want. Also, if the arrays are all ordered lists then we can make assumptions and ignore the value of p. The following code does no error checking, makes assumptions about your data format, and I apologize if my ruby coding style makes anyone's eyeballs bleed, but it should give you an idea of what your code would need to do...

```
    xml { source => "message" target => "[@metadata][theXML]" force_array => false }
    ruby {
        code => '
            xml = event.get("[@metadata][theXML]")
            types = xml["measType"]
            values = xml["measValue"]
            a = []
            values.each { |x|
                h = {}
                h["measObjLdn"] = x["measObjLdn"]
                x["r"].each_index { |i|
                    h[types[i]["content"]] = x["r"][i]["content"]
                }
                a << h
            }
            event.set("data", a)
        '
    }

```

will produce

```
      "data" => [
    [0] {
                  "VS.MemUsedSize" => "26777.00",
             "VS.MemAvailableSize" => "361.00",
                      "measObjLdn" => "PoolType=necc, PoolId=0, PoolMember=0, Machine=XXXXXX1, UUID=ABCDABCD1",
        "VS.ScdrsTranferredOverBp" => "0",
                 "VS.swapMemUsage" => "100.00"
    },
    [1] {
                  "VS.MemUsedSize" => "20214.00",
             "VS.MemAvailableSize" => "312.00",
                      "measObjLdn" => "PoolType=necc, PoolId=0, PoolMember=1, Machine=XXXXXX2, UUID=ABCDABCD2",
        "VS.ScdrsTranferredOverBp" => "0",
                 "VS.swapMemUsage" => "100.00"
    },
    [2] {
                  "VS.MemUsedSize" => "18203.00",
             "VS.MemAvailableSize" => "15213.00",
                      "measObjLdn" => "PoolType=necc, PoolId=0, PoolMember=2, Machine=XXXXXX3, UUID=ABCDABCD3",
        "VS.ScdrsTranferredOverBp" => "0",
                 "VS.swapMemUsage" => "100.00"
    }
],
```

---

_[View the full topic](https://discuss.elastic.co/t/ruby-iteration-in-logstash-help/224054)._
