Object to JSON in Mule 4 | Object to JSON

Object to JSON in Mule 4 | Object to JSON


Object to JSON Conversion in Mule 4

In mule 4 java objects are converted to other formats like JSON, XML by transform message components.
Inflow below the first XML is converted into java objects then java objects to JSON.

flow:


First transform message converts XML to java objects
Second transform message converts java objects to JSON.

First transform message configuration:


Second transform message configuration:


Pojo java class stored in src/main/java:
package test_objecttojson;

public class Test {
      
       private int id;
       private String name;
      
       public int getId() {
              return id;
       }
       public void setId(int id) {
              this.id = id;
       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
      
      

}


Method: POST

Input and output:


XML project code:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core" xmlns:http="http://www.mulesoft.org/schema/mule/http"
       xmlns="http://www.mulesoft.org/schema/mule/core"
       xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd">
       <http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="e64b4d41-94ef-47da-af3f-f2ebd9e0262a" >
              <http:listener-connection host="0.0.0.0" port="8085" />
       </http:listener-config>
       <flow name="test_objecttojsonFlow" doc:id="db110d0d-3d65-4533-b397-3c0aad33b379" >
              <http:listener doc:name="Listener" doc:id="8c3a176d-c362-4088-9465-88faebdce7e7" config-ref="HTTP_Listener_config" path="/java"/>
              <ee:transform doc:name="Transform Message" doc:id="a8f2560e-64b4-4c3f-b7b0-30516ee21b7a" >
                     <ee:message >
                           <ee:set-payload ><![CDATA[%dw 2.0
output application/java
---
{
      
       id: payload.data.id,
       name: payload.data.name
} as Object {
       class : "test_objecttojson.Test"
}]]></ee:set-payload>
                     </ee:message>
              </ee:transform>
              <ee:transform doc:name="Transform Message" doc:id="068a4b84-e3f2-41e0-a6b3-deafca4511cb" >
                     <ee:message >
                           <ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
       id: payload.id default 0,
       name: payload.name default ""
}]]></ee:set-payload>
                     </ee:message>
              </ee:transform>
       </flow>
</mule>