Converting JSON to XML in the OSB when consuming a REST service.

In my previous post I showed you how you could consume the Google Geo service using the OSB. In that example we used XML as the return format. It was also possible to return JSON. JSON stands for JavaScript Object Notation and is smaller then XML and faster en easier to parse. The JSON text format is syntactically identical to the code for creating JavaScript objects. That is why it is used more often now by frontend frameworks such as Apache Isis for example.

In this post I will show you how you can parse the JSON returned from Google using the OSB. To convert XML to JSON and JSON to XML, you can create a helper class in Java which can do this for you. Make the convenience methods public and static so you can use them in a Java Callout in the OSB. My class looks like this:

package nl.redrock.common;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.xml.XMLSerializer;

 * This class hold a couple of static methods which can be used to convert xml to json and json into xml.
 * @author Hugo Hendriks
public class XMLJSONConverter {

	 * Converts a Json String to a XML string
	 * @param aRootName the name of the root element in the xml
	 * @param aJson the json string
	 * @return null or a string which holds the xml representation of the json string
	 * @throws Exception
	public static String convertJSON2XML(String aRootName, String aJson) throws Exception {
		String result = null;

        XMLSerializer xmlSerializer = new XMLSerializer();
        JSON json = JSONSerializer.toJSON( aJson );
        result = xmlSerializer.write( json );
        return result;

	 * Convert a XML string to a json string
	 * @param aXml the XML string
	 * @return null or a string which holds the json representation of the xml string
	 * @throws Exception
	public static String convertXML2JSON(String aXml) throws Exception {
		String result = null;

		XMLSerializer xmlSerializer = new XMLSerializer();
		JSONObject json = (JSONObject);
		result = json.toString();
        return result;


I use maven as my build tool and my pom file looks like this:

<project xmlns="" xmlns:xsi=""
	<description>This library can be used to convert XML into JSON and back again</description>



You can see I am using the Maven dependency plugin to create a directory which holds all the dependencies jars. This is done because I need a few of these libraries on the weblogic server. In the MY_DOMAIN/lib directory to be precise.

This is the case for:

  • json-lib-2.4-jdk15.jar
  • ezmorph-1.0.6.jar
  • xom-1.2.5.jar

You can get these from the libs directory created by maven when you run the mvn -clean -package target. The other libraries are already available in Weblogic.

After this, you can add your XMLJSONConverter library to your project.

Then you can make a java callout in which you can convert the received json into a xml string.

After that, you can parse the string to real xml by making use of the fn-bea:inlinedXML() function.

So now you know how you can convert JSON to XML and vice versa.

Example was made in 11G PS4

3 Replies to “Converting JSON to XML in the OSB when consuming a REST service.”

  1. Hugo, thanks for the post, I’m going to implement the Googe maps/REST approach in my latest project.

    One question though. I’m doing another java callout in a seperate project. The jar I’m importing references apache commons.lang 2.4. When I execute the method on the OSB server, I get an error:

    java.lang.NoSuchMethodError: org.apache.commons.lang.StringUtils.join

    similar to this problem:

    I’m guessig it’s due to the fact that Weblogic uses an older version of commons.lang and it’s getting picked up earlier in the classpath. I added all of my jars to $DOMAIN_HOME/libs and bounced my servers.

    I noticed you are using commons.lang 2.5, did you run into this issue at all?

    hoping for a reply.

  2. I did not run into these kind of issue to be honest. I let Maven manage my dependencies so I knew this would not give a problem. Like you said, it is usually a classpath problem with weblogic that loads pre-packaged jars. To my astonishment, it worked straight away when i put the libs on the server and tried to do the java callout. So u might wanna try it with commons.lang 2.5

  3. Pingback: 2012 | Hugo Hendriks @ | jmmate JavaBlog

Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.