Skip to content

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.

JSON to XML

Step 1: Create a new integration project

  1. Click on the Ballerina Integrator icon on the sidebar.
  2. Click on the Create Integration button.
  3. Enter the project name as JsonToXml.
  4. Select project directory location by clicking on the Select Location button.
  5. Click on the Create Integration button to create the integration project.

    Create Integration

Step 2: Create a HTTP service

  1. In the design view, click on the Add Artifact button.
  2. Select HTTP Service under the Integration as API category.
  3. Select the Create and use the default HTTP listener option from the Listener dropdown.
  4. Select Design from Scratch option as the The contract of the service.
  5. Specify the Service base path as /convert.
  6. Click on the Create button to create the new service with the specified configurations.

Step 3: Update the resource method

  1. 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.
  2. Then click the edit button in front of /greeting resource.

    Edit Resource

  3. Change the resource HTTP method to POST.

  4. Change the resource name as toXml.
  5. Add a payload parameter named input to the resource of type json.
  6. Change the 201 response return type to xml.
  7. Click on the Save button to update the resource with the specified configurations.

    Update Resource

Resource Method

To learn more about resources, see Ballerina Resources.

Step 4: Add the transformation logic

  1. Click on the toXml resource to navigate to the resource implementation designer view.
  2. Delete the default Return action from the resource.
  3. Hover to the arrow after start and click the ➕ button to add a new action to the resource.
  4. Select Function Call from the node panel.
  5. Search for json to xml and select the fromJson function from the suggestions.
  6. Change the Variable Name to xmlResult, Variable Type as xml and JsonValue to input.
  7. Click on the Save button to add the function call to the resource.

    Add Function Call

  8. Add a new node after the fromJson function call and select Return from the node panel.

  9. Select the xmlResult variable from the dropdown and click Save.

    Add Return

JSON to XML Conversion

To learn more about json to xml conversion, see Ballerina JSON to XML conversion.

Step 5: Run the integration

  1. Click on the Run button in the top-right corner to run the integration.
  2. The integration will start and the service will be available at http://localhost:9090/convert.
  3. Click on the Try it button to open the embedded HTTP client.
  4. Enter the JSON payload in the request body and click on the ▶️ button to send the request.
    {
        "name": "John",
        "age": 30,
        "car": "Honda"
    }
    
  5. The response will be an XML representation of the JSON payload.
    <root> <name>John</name> <age>30</age> <car>Honda</car> </root> Run Integration

  6. 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"}'