NXSD is used to convert JSON to XML and vice versa. In this post will see the example to convert the JSON sample into XML sample.
Before NXSD, to convert from JSON structure to XML structure or vice versa, mostly java callout was being used. The SOA Suite has the capability to come up with an XML representation of data in all kinds of formats that are not XML. Native XML Schema Definitions i.e nXSD is used to convert the native format to XML structure and vice versa.
At any moment when in a Service Bus Pipeline we are dealing with data that is structured – but not in XML format, so it will not be accessible to our pipeline, for example, JSON, Fixed Position format or CSV. We can use the native mapping to convert from the native structure to XML. The reverse operation is also available with the NXSD mapping: from XML to the native format specified.
So, here will create an application which will convert JSON structure to XML structure.
Go to File -> New -> Application
From available options, select Service Bus Application.
Enter Application Name: OSBProjects and click Finish
To create, new Service Bus Project, Go to File -> New -> Project
From available options, select Service Bus Project.
Enter Project Name : NXSD_JsonToXML and click Finish.
To create the folder structure in the project right click on project name i.e. NXSD_JsonToXML Go to New -> From Gallery
Go to Folder option and click on OK
Enter folder name : Pipeline and click on OK
As mentioned in the previous step create two more folders: ProxyService and Schemas
To create the pipeline, Right click on Pipeline folder and Go to New -> Pipeline
Enter Service Name : PS_NXSD_JsonToXML_Pipeline and click on Next
Click on REST option and Expose as a Proxy Service option. Change the Proxy Location to ProxyService Folder and click on Finish.
Double click on the PS_NXSD_JsonToXML_Pipeline and Drag pipeline pair node from the component pallet.
To create the NXSD schema, Right click on Schemas folder and Go to New -> NXSD Schema
Native format builder will open, click on Next
Enter file Name for XSD Schema : JsonToXML.xsd
Specify the format type as : JSON Interchange Format and click on Next
Enter the JSONRequest in Sample and click on Next. Whatever JSON sample you want to convert in XML you can enter here or you can select the file which contains JSON sample by using browse option. Here you have the option to select your root element name also. By default it is Root-Element.
It will generate automatically XML schema i.e. nothing but the XSD for your JSON sample. Click on Next
It will show generated schema location. Click on Finish.
As shown below, XSD will be create to you native format i.e. JSON sample.
From component pallet select nXSD translate activity and drag it into Request pipeline. As we are converting JSON to XML so in Translate option will select Native to XML option.
In Input, we need to give the expression which has a JSON sample. So here in the body, we are passing a JSON sample. So will enter expression value as $body/text().
After that, we need to select the nXSD Schema. Here we have two options static and dynamic. As we already have generated the schema so will select the static option and browse the respective XSD i.e. JsonToXML.xsd file from schemas folder. Then we have an option to select the output variable. Here you can select any variable or body variable. As I want data to be saved in body variable only, so I choose body variable.
To convert JSON sample to XML, our code is ready. So will deploy the code.
Select option Deploy to Service Bus Server and click on Next.
Select the configured Application Server i.e. Integrated Weblogic Server.
On Service Bus Console, navigate to your project, pipeline folder and double click on the pipeline. Click on Green arrow to test the pipeline.
elect Media Type as application/json. Enter payload which you want to convert from JSON to XML. Make sure you use the same JSON format which you have used at the time of creation on nXSD. Click on Execute.
The output will be as shown below.
No comments:
Post a Comment