IBM Support

How to transform XML to JSON in Java Compute Node

Question & Answer


Question

How to transform XML to JSON in Java Compute Node

Answer

On Demand Consulting
Author: Ming Li

Abstract

Consider a scenario that you want to transform your XML message to JSON format whose schema is dynamical and cannot be defined in a XSD file. Then you can not just use a map node to do such transformation.
This article mainly describes how to transform an input with XML format to an output with JSON format in a different schema.

Basic Knowledge

1. How to create a given JSON object with MbElement

Here is the given JSON object.
 
  { "Books": [   ?  { "Name": "Steve Jobs",   ?    "ISBN": "1451648537",   ?    "Language": "English",   ?    "Format": [ "Hardcover", "Paperback", "Audiobook CD", "Audible" ] },   ?  { "Name": "Beautiful Whale",   ?    "ISBN": "1419703846",   ?    "Language": "English",   ?    "Format": [ "Hardcover" ] }   ] }  

Here is the code to create this JSON object
  MbElement jsonEle = outMessage.getRootElement().createElementAsLastChild(MbJSON.PARSER_NAME);   MbElement dataEle = jsonEle.createElementAsLastChild(MbJSON.OBJECT, MbJSON.DATA_ELEMENT_NAME, null);   MbElement booksEle = dataEle.createElementAsLastChild(MbJSON.ARRAY, "Books", null);   MbElement book1Ele = booksEle.createElementAsLastChild(MbJSON.OBJECT, MbJSON.ARRAY_ITEM_NAME, null);   book1Ele.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Name", "Steve Jobs");   book1Ele.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "ISBN", "1451648537");   book1Ele.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Language", "English");   MbElement book1FormatEle = book1Ele.createElementAsLastChild(MbJSON.ARRAY, "Format", null);   book1FormatEle.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, MbJSON.ARRAY_ITEM_NAME, "Hardcover");   book1FormatEle.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, MbJSON.ARRAY_ITEM_NAME, "Paperback");   book1FormatEle.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, MbJSON.ARRAY_ITEM_NAME, "Audiobook CD");   book1FormatEle.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, MbJSON.ARRAY_ITEM_NAME, "Audible"); MbElement   book2Ele = booksEle.createElementAsLastChild(MbJSON.OBJECT, MbJSON.ARRAY_ITEM_NAME, null);   book2Ele.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Name", "Beautiful Whale");   book2Ele.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "ISBN", "1419703846");   book2Ele.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, "Language", "English"); MbElement book2FormatEle =   book2Ele.createElementAsLastChild(MbJSON.ARRAY, "Format", null);   book2FormatEle.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, MbJSON.ARRAY_ITEM_NAME, "Hardcover");

2. How to access values in a JSON object
  // Get the first book's name - "Steve Jobs"  String book1Name = message.getRootElement().getFirstElementByPath("JSON/Data/Books/Item/Name").getValueAsString();  See KLhttps://odc.supportportal.com/link/portal/5377/31053/Article/27994/How-to-use-XPath-with-JSON-messages-in-Java-nodes  			  // Get the second book's name - "Beautiful Whale"  MbElement booksEle = message.getRootElement().getLastChild().getLastChild().getLastChild();   String book2Name = booksEle.getFirstChild().getNextSibling().getFirstChild().getValueAsString();      
Transformation Sample


Requirement

All XML data is queried from a database, here is the database table

Empid Department FirstName LastName
1 Accounts John Doe
2 Accounts Ram Kumar
3 Accounts Joe Foster
4 Risk John Rowe
5 Risk Rob Conley
6 IT David Dickson
7 IT James Clear
8 IT Phil Viktor
9 IT Alfred Rich

The goal is to transform XML data to a JSON format as below. ?And the department names in JSON object are not supposed to be hard coded.

{
Accounts:{
Person:{
Empid: "1",
FirstName: "John",
LastName: "Doe"
}
Person:{
Empid: "2",
FirstName: "Ram",
LastName: "Kumar"
}
Person:{
Empid: "3",
FirstName: "Joe",
LastName: "Foster"
}
}
Risk:{
Person:{
Empid: "4",
FirstName: "John",
LastName: "Rowe"
}
Person:{
Empid: "5",
FirstName: "Rob",
LastName: "Conley"
}
}
IT:{
Person:{
Empid: "6",
FirstName: "David",
LastName: "Dickson"
}
Person:{
Empid: "7",
FirstName: "James",
LastName: "Clear"
}
Person:{
Empid: "8",
FirstName: "Phil",
LastName: "Viktor"
}
Person:{
Empid: "9",
FirstName: "Alfred",
LastName: "Rich"
}
}
}


Message Flow

We create the message flow starting with an HTTP Input and a map node to simulate the DB query. Then use Java Compute Node to do the transformation. Finally get the output JSON data from an HTTP Reply.
User-added image

User-added image
Map


Code in Java Compute Node

Here is the transformation code



Test it in Flow Exerciser
User-added image
User-added image


Reference

Creating a JSON message
https://www.ibm.com/support/knowledgecenter/en/SSMKHH_10.0.0/com.ibm.etools.mft.doc/bc28411_.htm

Accessing elements in a message tree from a JavaCompute node
https://www.ibm.com/support/knowledgecenter/en/SSMKHH_10.0.0/com.ibm.etools.mft.doc/ac30330_.htm

Appendix

You can find it in attachment to see the whole example.

[{"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Product":{"code":"SSQTW3","label":"IBM On Demand Consulting for Hybrid Cloud"},"Component":"","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"All Versions","Edition":"","Line of Business":{"code":"","label":""}}]

Document Information

More support for:
IBM On Demand Consulting for Hybrid Cloud

Software version:
All Versions

Document number:
776417

Modified date:
17 March 2019

UID

ibm10776417

Manage My Notification Subscriptions