Message Transformation¶
Overview¶
This guide explains how to create a simple integration to convert a JSON payload to an XML payload using WSO2 Integrator: BI.
An HTTP service with a single resource (toXml) will be created to accept a JSON payload and return the XML representation of the payload.
Step 1: Create a new integration project¶
- Click on the BI icon on the sidebar.
- Click on the Create New Integration button.
- Enter the integration name as
JsonToXml. - Select project directory location by clicking on the Select Location button.
-
Click on the Create Integration button to create the integration project.
Step 2: Create an HTTP service¶
- In the design view, click the Add Artifact button.
- Select HTTP Service under the Integration as API category.
- Select the Design From Scratch option for the Service Contract.
- Specify the Service Base Path as
/convert. -
Click the Create button to generate the new service with the specified configurations.
Step 3: Add the resource method¶
- Click the Add Resource button.
- Select the POST method from the right side panel.
- Enter the resource path as
toXml. - Click on the Define Payload action and select Continue with JSON Type.
- Change the 201 response return type to
xmland Save response. -
Click on the Save button with the specified configurations.
Resource Method
To learn more about resources, see Ballerina Resources.
Step 4: Add the transformation logic¶
- Click on the
toXmlresource to navigate to the resource implementation designer view. - Hover over the arrow after the start and click the ➕ button to add a new action to the resource.
- Select Function Call from the node panel.
- Search for
json to xmland select the fromJson function from the suggestions. - Set the JsonValue to
payloadfrom inputs. -
Click on the Save button to add the function call to the resource.
-
Add a new node after the
fromJsonfunction call and select Return from the node panel. -
Select the
xmlResultvariable from the dropdown and click Save.
JSON to XML Conversion
To learn more about json to xml conversion, see Ballerina JSON to XML conversion.
Step 5: Run the integration¶
- Click on the Run button in the top-right corner to run the integration.
- The integration will start and the service will be available at
http://localhost:9090/convert. - Click on the Try it button to open the embedded HTTP client.
- Enter the JSON payload in the request body and click on the ▶️ button to send the request.
{ "name": "John", "age": 30, "car": "Honda" } -
The response will be an XML representation of the JSON payload.
<root> <name>John</name> <age>30</age> <car>Honda</car> </root>
-
Additionally, the service can be tested using tools like Postman or curl by sending a POST request with a JSON payload to the service endpoint.
curl -X POST "http://localhost:9090/convert/toXml" -H "Content-Type: application/json" -d '{"name":"John", "age":30, "car":"Honda"}'





