# Convert json to xml

**URL:** https://discuss.elastic.co/t/convert-json-to-xml/103863
**Category:** Logstash
**Created:** [October 13, 2017, 9:21am UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863 "2017-10-13T09:21:31Z")
**Posts on this page:** 18
**Page:** 1

<div class="post-metadata">

### Author: ![rm1890](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@rm1890](https://discuss.elastic.co/u/rm1890)
#### Post date: [October 13, 2017, 9:21am UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/1 "2017-10-13T09:21:31Z")

</div>

hi all,

I wonder if there is a way to convert json to xml. So, in my json data one of the fields has string type. It is actually xml content but represented as string in double quotes. Is there any best practice to convert/extact that field and process as xml?

Cheers

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [October 13, 2017, 12:19pm UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/2 "2017-10-13T12:19:37Z")

</div>

It's not clear to me exactly what you want to do, but the xml filter can parse XML into Logstash fields.

---

<div class="post-metadata">

### Author: ![rm1890](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@rm1890](https://discuss.elastic.co/u/rm1890)
#### Post date: [October 13, 2017, 12:27pm UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/3 "2017-10-13T12:27:29Z")

</div>

ok, so, I want to convert the json message into xml so that the parse could work. Currently, in the json schema I have 3 fields: ID - int, XML - string, Modified - date. The issue is that the XML field has type string but as far as I know xml parser expects XML type and not string. This is the reason that I want to convert the string to xml type and only then apply the parsing.

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [October 13, 2017, 1:26pm UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/4 "2017-10-13T13:26:35Z")

</div>

There is no such thing as an XML type. The xml filter parses strings that contain XML, i.e. exactly what you have.

---

<div class="post-metadata">

### Author: ![rm1890](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@rm1890](https://discuss.elastic.co/u/rm1890)
#### Post date: [October 13, 2017, 1:41pm UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/5 "2017-10-13T13:41:35Z")

</div>

I'm trying to apply xpath on the message, but somehow it doesn't extract the desired fields. Looks like it ignore what is written in xml part of the config file.

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [October 13, 2017, 2:09pm UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/6 "2017-10-13T14:09:32Z")

</div>

If you want help you need to provide your configuration and an example event.

---

<div class="post-metadata">

### Author: ![rm1890](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@rm1890](https://discuss.elastic.co/u/rm1890)
#### Post date: [October 13, 2017, 2:35pm UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/7 "2017-10-13T14:35:02Z")

</div>

ok, so here is the complete json message with 3 fields. I want to extract XML field. You can see the content of XML field as well. I created an example.

{"Schema":{"type":"struct","fields":[{"type":"int32","optional":false,"field":"id"},{"type":"string","optional":true,"field":"xml"},{"type":"int64","field":"modified"}],"optional":false},"payload":{"id":56,"xml":"Xmlns:test\"http://example.com\"\<test:data1 type="55"\>\<test:text test:content="222"/\>\</test:data1\>\<test:year test:content="1999"/\>\</test:year\>","modified":1507908845773}}

the filter in my config that I'm using is the following:

filter {

```
      xml {
            store_xml => "false"
            source => "message"
            remove_namespaces => "false"

            xpath =>[
                            "/data1/type/text()","type",

                            "/year/content/text()","year",
                            ]

```

}  
}

So, after applying the config I see the same json message content. Also, is there any possibility in logstash to remove the backslashes which are there for whitespaces in my xml?

Cheers

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [October 16, 2017, 6:12am UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/8 "2017-10-16T06:12:54Z")

</div>

You're attempting to parse the `message` field as XML but it appears the XML is actually in the `[payload][xml]` field.

---

<div class="post-metadata">

### Author: ![rm1890](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@rm1890](https://discuss.elastic.co/u/rm1890)
#### Post date: [October 16, 2017, 7:43am UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/9 "2017-10-16T07:43:43Z")

</div>

is there any possibiltiy to apply a filter on payload?

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [October 16, 2017, 8:04am UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/10 "2017-10-16T08:04:41Z")

</div>

It depends on what you want to do but the answer is probably yes.

---

<div class="post-metadata">

### Author: ![rm1890](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@rm1890](https://discuss.elastic.co/u/rm1890)
#### Post date: [October 16, 2017, 8:08am UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/11 "2017-10-16T08:08:10Z")

</div>

I acutally want to extract some values from [payload] : [XML]. For instance, the type and year es mentioned in previous example. I assume XPATH won't be able to extract the data directly from payload, so I need other filters perhaps?

Cheers

---

<div class="post-metadata">

### Author: ![rm1890](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@rm1890](https://discuss.elastic.co/u/rm1890)
#### Post date: [October 16, 2017, 9:20am UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/12 "2017-10-16T09:20:08Z")

</div>

ok, I'm able to extract now the xml field from payload using the following filter in config file:

```
    filter {
       json {
        source => "message"
        target => "parsedMain"
    }
    json {
        source => "[parsedMain][payload][xml]"
        target => "parsedContent"
    }
}

```

So, now I'm trying to use the json output, which is in this case the target "parsedContent", as a source for xml.

```
   xml {
            store_xml => "false"
            source => "parsedContent"
            remove_namespaces => "false"

            xpath =>[
                            "/data1/type/text()","type",

                            "/year/content/text()","year",
                            ]
}

```

It doesn't work though. I assume I'm doing something wrong?

Cheers

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [October 16, 2017, 10:26am UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/13 "2017-10-16T10:26:37Z")

</div>

Why are you trying to parse `[parsedMain][payload][xml]` as JSON? That's where the XML data is.

---

<div class="post-metadata">

### Author: ![rm1890](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@rm1890](https://discuss.elastic.co/u/rm1890)
#### Post date: [October 16, 2017, 10:52am UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/14 "2017-10-16T10:52:38Z")

</div>

ok, I removed the json part and now parsing the `[parsedMain][payload][xml]` as XML, bust somehow it ignores the xpath. I mean it doesn't extract the data from XML.

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [October 16, 2017, 11:41am UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/15 "2017-10-16T11:41:46Z")

</div>

Perhaps the XPath query doesn't match the data? Or do you need to set `remove_namespaces => true`?

---

<div class="post-metadata">

### Author: ![rm1890](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@rm1890](https://discuss.elastic.co/u/rm1890)
#### Post date: [October 16, 2017, 3:16pm UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/16 "2017-10-16T15:16:51Z")

</div>

It doesn't even remove the namespaces.

---

<div class="post-metadata">

### Author: ![rm1890](https://avatars.discourse-cdn.com/v4/letter/r/85e7bf/32.png) [@rm1890](https://discuss.elastic.co/u/rm1890)
#### Post date: [October 17, 2017, 11:56am UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/17 "2017-10-17T11:56:01Z")

</div>

ok, just to let you know, it is solved. The solution was to use @ instead of text(). So, in my example the xml filter should have been like this:

```
xml {
            store_xml => "false"
            source => "parsedContent"
            remove_namespaces => "false"

            xpath =>[
                            "/data1/@type","type",

                            "/year/@content","year",
                            ]
}

```

Cheers

---

<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 14, 2017, 11:56am UTC](https://discuss.elastic.co/t/convert-json-to-xml/103863/18 "2017-11-14T11:56:08Z")

</div>

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