Reputation: 51
I'building a proxy-service using wso2 esb. It sends a REST request to Google Books API and receives json. In this Json, there's a dynamic array which I have to parse to XML. I can't seem to figure out how can I do that.
received json payload
"items": [
{
"volumeInfo": {
"title": "Web Services",
"authors": [
"Gustavo Alonso",
"Fabio Casati",
"Harumi Kuno",
"Vijay Machiraju"
],
"publisher": "Springer Science & Business Media",
"publishedDate": "2003-09-04"
]
}
If you glance over the above received Json, it's something like this items[0].authors[i]
here authors[i] is very dynamic, as different books have different number of authors.
How can I convert this payload to XML and then send as an XML to client
<items>
<titie></title>
<authors>
<author></author>
<author></author>
<author></author>
.
.
.
</authors>
</items>
Upvotes: 2
Views: 727
Reputation: 419
When converting from JSON to XML I almost always use this way.
First set messageType to xml, you may also set ContentType but I am not 100% sure if it is required
<property name="messageType" scope="axis2" value="application/xml"/>
<property name="ContentType" scope="axis2" value="application/xml"/>
Next use the payload factory with an enclosing root element, set the media type to XML
<payloadFactory media-type="xml">
<format>
<SomeRequest xmlns="yourXMLNamespace">
$1
</SomeRequest>
</format>
<args>
<arg evaluator="json" expression="."/>
</args>
</payloadFactory>
Now you will have something that looks like this.
<SomeRequest>
<items>
<volumeInfo></volumeInfo>
<title>Web Services</title>
<authors>Gustavo Alonso</authors>
<authors>Fabio Casati</authors>
<authors>Harumi Kuno</authors>
<authors>Vijay Machiraju</authors>
<publisher>Springer Science & Business Media</publisher>
<publishedDate>2003-09-04</publishedDate>
</items>
<items>
...
</items>
</SomeRequest>
See how it unwrapped the JSON array, it created multiple elements all using the array name. To get from here to the response format you want the easiest way to do it is using an xslt transform.
<xslt key="{name of your xslt transform file}"/>
Then you can respond to back to the client.
It may be worthwile checking out the JSON Support page in the wso2 docs. It covers how JSON is converted to and from XML https://docs.wso2.com/display/EI620/JSON+Support
Upvotes: 1
Reputation: 2209
In the outsequence you can use a payload mediator to construct the xml from JSON.
https://docs.wso2.com/display/ESB500/PayloadFactory+Mediator
Upvotes: 0