Message Transformation¶
Overview¶
This guide explains how to create a simple integration to convert a JSON payload to an XML payload using Ballerina Integrator. 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 Ballerina Integrator icon on the sidebar.
- Click on the Create Integration button.
- Enter the project 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 a HTTP service¶
- In the design view, click on the Add Artifact button.
- Select HTTP Service under the Integration as API category.
- Select the Create and use the default HTTP listener option from the Listener dropdown.
- Select Design from Scratch option as the The contract of the service.
- Specify the Service base path as
/convert
. - Click on the Create button to create the new service with the specified configurations.
Step 3: Update the resource method¶
- The service will have a default resource named
greeting
with the GET method. Click on three dots appear in front of the/convert
service resource and select Edit from menu. -
Then click the edit button in front of
/greeting
resource. -
Change the resource HTTP method to POST.
- Change the resource name as
toXml
. - Add a payload parameter named
input
to the resource of typejson
. - Change the 201 response return type to
xml
. -
Click on the Save button to update the resource with the specified configurations.
Resource Method
To learn more about resources, see Ballerina Resources.
Step 4: Add the transformation logic¶
- Click on the
toXml
resource to navigate to the resource implementation designer view. - Delete the default
Return
action from the resource. - Hover to the arrow after start and click the ➕ button to add a new action to the resource.
- Select Function Call from the node panel.
- Search for
json to xml
and select the fromJson function from the suggestions. - Change the Variable Name to
xmlResult
, Variable Type asxml
and JsonValue toinput
. -
Click on the Save button to add the function call to the resource.
-
Add a new node after the
fromJson
function call and select Return from the node panel. -
Select the
xmlResult
variable 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"}'