Parsing a JSON String and Converting it to XML in TIBCO



If you have the REST & JSON Plug-in for TIBCO BusinessWorks, parsing and rendering a JSON string into XML is a straightforward task. We are often using XML in TIBCO, because XML is the most common format that is used to transform data using XSLT and XPath language. There are two ways we can parse and render JSON text: using an XML Schema, and using Java classes. Of course, XML Schema is already very handy, as we can always easily create one in TIBCO Designer. But, before we go to why we need to use Java classes, we need to examine first the nature of an XML Schema. An XSD describes the XML document in a strictly structured manner, which means that the sequence and nesting of the elements are important. However, for JSON, we cannot rely on the order of the elements, and we cannot force JSON to be that way. It’s just its character, and it maintains itself as an unordered collection of zero or more name/value pairs. That said, we cannot expect a JSON message to conform to the specification of an XML Schema all the time. This the constraint when parsing a JSON message using an XML Schema. Although we can disable output validation as a workaround, it may not be the most reliable solution, and we are at risk of losing data for certain elements. Hence, I had been more convinced to use Java code instead, NOT the Java Classes as Schema Type for ParseJSON activity. I had encountered one limitation when using Java Classes, and that is, it uses the class name as the root element and converts it to lowercase characters. Another problem is that we cannot expect the root element to be lowercase all the time, and without further ado, I used a Java serialization library to convert Java objects into JSON and back, called Gson.
Gson exposes methods for JSON manipulation in the simplest way: toJson() and fromJson(). It’s quite easy to learn about these methods. There was one challenge I had, and that is how to create the Java Beans or the POJOs. By all means, I can do it manually, and it’s not difficult, but what if I already have an XML Schema? So I thought about using the xjc utility that comes with the JDK installation. If you already have an XSD or a WSDL file, you can use this utility to generate the Java classes. The command line looks like as follows:
                xjc –p com.tibco.beans –xmlschema Schema.xsd
You can then import the package into an Eclipse project, or compile the Java classes manually using the javac utility, and then create a jar file using the jar utility. I assume you’re already aware of this, but just one watch-out is to ensure that you have set backward compatibility according to the Java runtime environment version you are using.
Last time, I wrote about converting XML to a Java object. The opposite of that is what we’re interested in when parsing a JSON text, but overall the things we need to do are pretty similar. Some things I have yet to modify after generating the Java Beans using xjc utility.
  1. The access for the class member fields, by default, is protected. I changed this to be private, to simply make it a POJO.
  2. The generated classes do not implement java.io.Serializable. I modified all classes to implement this interface. Also, child nodes are created as public inner classes, but I didn’t find them to be a problem so far.
  3. The ObjectFactory, which is, I supposed, the root class, does not have a member field and a setter/getter. I added a member field that represents the root element.
  4. Arrays are created as Collection types such as java.util.ArrayList. TIBCO serialization from Java object to XML does not support Collection types, so I had to change the types of repeating nodes to a simple Array.
We are going to use the fromJson method of the Gson library to convert a JSON string to a Java object. The method accepts two parameters: the JSON string, and the Class object that represents the JSON string. Gson, as I have read, uses the Java reflection methods to determine the name/value pairs. We need to ensure that all the member field names match the fields in the JSON string; otherwise, only those that match will be retrieved.
Now, I need to be able to call the fromJson method in the process definition. One way is to use the Java Method activity, and then pass the output Object as the input to the Java to XML activity.


The Java Method requires an Alias Library shared resource that contains the class path to the JAR file. The JAR file must contain the Java class and method that you want to invoke. This method needs to be public and static, and it must return a value; in other words, not void. Return types supported are primitive types and Object, but Collection types are not supported. You may refer to the documentation for specific details.  Shown below is the method that we want to call. The method accepts two parameters: the first parameter is the JSON string, and the second parameter is the fully qualified name of the Java Bean. Note that you also have to include the class path of the JAR file that contains the JavaBeans that were previously created, and this can be added to the Alias Library as well.
 

Configuration for the  Java Method activity.


Input for the Java Method activity.
  
 Output for the Java to XML activity.
 
This process definition will convert JSON strings to XML which can easily be manipulated in TIBCO using XSLT and XPath language. Of course, you can also use the output Object in a Java Code activity for further processing. There are just many possibilities, and in TIBCO, we are not often using other data formats apart from XML, may it be JSON, Java Objects, or flat files. Every data structure is represented as an XML, and this is just one approach to maximize TIBCO’s extensive capabilities for XML manipulation.

Comments

  1. Can you please provide source code ...

    ReplyDelete
  2. I need to convert xml to Json in a similar way ... can you please help
    Rohit

    ReplyDelete
  3. Nice information can you elaborate topic on conversation.TIBCO

    ReplyDelete
  4. I got fresh and exclusive information on this website. I always tried to learn something new. I am glad to see this post. Thank you so much for sharing this information with us.
    TIBCO BW Container Edition Online Training
    TIBCO BWCE training in Ameerpet
    TIBCO BWCE training in Hyderabad

    ReplyDelete

Post a Comment

Popular posts from this blog

XML Schema and JSON Schema Validation in Mule 4

Using XML To Java in TIBCO BW