# Help parsing XML-filter processed data with ruby

**URL:** https://discuss.elastic.co/t/help-parsing-xml-filter-processed-data-with-ruby/176841
**Category:** Logstash
**Created:** [April 15, 2019, 6:42am UTC](https://discuss.elastic.co/t/help-parsing-xml-filter-processed-data-with-ruby/176841 "2019-04-15T06:42:20Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![makibroshett](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/makibroshett/32/106322_2.png) [@makibroshett](https://discuss.elastic.co/u/makibroshett)
#### Post date: [April 15, 2019, 6:42am UTC](https://discuss.elastic.co/t/help-parsing-xml-filter-processed-data-with-ruby/176841/1 "2019-04-15T06:42:20Z")

</div>

Hi,

I need some help writing seemingly simple ruby code, but I can't figure out how (newbie).  
At some point, my pipeline produces a field structured as follows::

```
   "xmldata" => {
"uplinkMessage" => {
    "constrainedData" => {
        "routeClearanceData" => {
            "RouteClearance" => {
                "routeInformations" => {
                    "publishedIdentifier" => [
                        [0] {
                            "fixName" => {
                                "name" => {
                                    "content" => "IRMAR"
                                }
                            }
                        },
                        [1] {
                            "fixName" => {
                                "name" => {
                                    "content" => "TAKES"
                                }
                            }
                        }
                    ]
                }
            }
        }
    }

```

I would like to reduce that collection of various fixName/name/content sub element values to a simple string: "IRMAR-TAKES-". But I can't get the ruby code right:

`[2019-04-12T16:06:05,791][ERROR][logstash.filters.ruby] Ruby exception occurred: undefined method`' for nil:NilClass`

Here is my code, thanks for any suggestion.

Olive

```
               ruby {
                code => '
                       route=""
                       rt=event.get("[xmldata][uplinkMessage][constrainedData][routeClearanceData][RouteClearance][routeInformations][publishedIdentifier]")
                       rt.each {|key,val|
                               route << val[key]["fixName"]["name"]["content"]
                               route <<"-"
                       }
                       event.set("Value",route)
                       '
        }
```

---

<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: [April 15, 2019, 12:21pm UTC](https://discuss.elastic.co/t/help-parsing-xml-filter-processed-data-with-ruby/176841/2 "2019-04-15T12:21:19Z")

</div>

You do not really need a ruby filter for that.

```
mutate { add_field => { "Value" => "%{[xmldata][uplinkMessage][constrainedData][routeClearanceData][RouteClearance][routeInformations][publishedIdentifier][0][fixName][name][content]}-%{[xmldata][uplinkMessage][constrainedData][routeClearanceData][RouteClearance][routeInformations][publishedIdentifier][1][fixName][name][content]}" } }

```

would work. That said, how about

```
    ruby {
        code => '
           route=""
           rt=event.get("[xmldata][uplinkMessage][constrainedData][routeClearanceData][RouteClearance][routeInformations][publishedIdentifier]")
           rt.each_index { |x|
                   route << rt[x]["fixName"]["name"]["content"]
                   route <<"-"
           }
           event.set("Value",route)
           '
    }
```

---

<div class="post-metadata">

### Author: ![makibroshett](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/makibroshett/32/106322_2.png) [@makibroshett](https://discuss.elastic.co/u/makibroshett)
#### Post date: [April 15, 2019, 5:09pm UTC](https://discuss.elastic.co/t/help-parsing-xml-filter-processed-data-with-ruby/176841/3 "2019-04-15T17:09:27Z")

</div>

Hi Badger,

The sequence of publishedIdentifier may contains more than 2 items, hence my insisting on iterating over that... collection ????

Thanks a lot for the help, I did not know that each\_index (I totally s...tart with ruby).

Now it works except for the trailing '-' that I know how to fix.

Again, thanks a LOOOT!

Olivier

---

<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: [April 15, 2019, 6:56pm UTC](https://discuss.elastic.co/t/help-parsing-xml-filter-processed-data-with-ruby/176841/4 "2019-04-15T18:56:44Z")

</div>

I would fix it using

```
event.set("Value",route.chomp("-"))
```

---

<div class="post-metadata">

### Author: ![makibroshett](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/makibroshett/32/106322_2.png) [@makibroshett](https://discuss.elastic.co/u/makibroshett)
#### Post date: [April 16, 2019, 6:53am UTC](https://discuss.elastic.co/t/help-parsing-xml-filter-processed-data-with-ruby/176841/5 "2019-04-16T06:53:42Z")

</div>

I did that too, thanks for the extra hint!

---

<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: [May 14, 2019, 6:53am UTC](https://discuss.elastic.co/t/help-parsing-xml-filter-processed-data-with-ruby/176841/6 "2019-05-14T06:53:46Z")

</div>

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