Over a million developers have joined DZone.

Payload Transformation: JSON to Java

DZone's Guide to

Payload Transformation: JSON to Java

Read on to view this tutorial in order to learn how to transform input data in the form of JSON to Java to increase efficiency and reduce execution time.

· Integration Zone ·
Free Resource

WSO2 is the only open source vendor to be named a leader in The Forrester Wave™: API Management Solutions, Q4 2018 Report. Download the report now or try out our product for free.

Mule provides us the functionality of transforming one input data type to another that not only increases the system efficiency but also reduces the execution time. The request is passed in different formats, namely XML, JSON, etc.

Here, in the code, the input data is passed in the form of JSON and transformed output as Java LinkedHashMap, which reduces the time of defining the Java class and referencing to it every time the request is sent. The HTTP Listener configuration can be set as port availability.

Image title

The request data sent is captured and set in the payload for Java transformation using Transform Message.

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

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" 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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
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/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="jsontojavaFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
        <set-payload value="#[payload]" doc:name="Set Payload"/>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
Name: payload.Name,
Profile: payload.Profile,
Module_date: payload.Module_date
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>

The transformation from JSON to Java is done and the result is logged using Logger.

Request URI: http://localhost:8081/jsontojava


"Name" : "Varun",
"Profile" : "Developer",
"Module_date" : "04/12/2017"
"Name" : "Puja",
"Profile" : "Tester",
"Module_date" : "08/11/2017"


Image title

The response is coming as per expectation, which not only increases code efficiency but also performance.

IAM is now more than a security project. It’s an enabler for an integration agile enterprise. If you’re currently evaluating an identity solution or exploring IAM, join this webinar.

mule ,json ,java ,xml ,integration

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}