How to transform json-to-json document in Mule ESB

2019-03-01 15:20发布

问题:

I have a flow with an HTTP endpoint that receives a json document. This should transformed into other json document: "json-to-json transformation. In mule ESB which is the best practice to do this without using an XSLT?

They could sharing some example.

Thanking your support.

回答1:

Other than Datamapper which is a Enterprise feature, you can use Expression transformer component to create your JSON payload.

So, when you receive the JSON payload after your HTTP, you parse the JSON using json-to-object-transformer (return type depends on your JSON), store the value of elements in variable, and finally use Expression transformer to create your dynamic JSON..

Let's consider a very simple example, let your JSON be

{
    "name": "Julio Cejas",
    "id": 37
}

Now,you want to transform into following JSON adding an extra element,

{
    "name": "Julio Cejas",
    "id": 37,
    "designation": "Director"
}

So, you can transform from one JSON to another dynamically using Expression transformer in the following flow :-

<flow name="DynamicJSONFlow1" doc:name="DynamicJSONFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8085" path="test" doc:name="HTTP"/>
         <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
         <set-variable variableName="name" value="#[message.payload.name]" doc:name="Variable"/>
         <set-variable variableName="id" value="#[message.payload.id]" doc:name="Variable"/>

         <expression-transformer
     expression="#[[ 
                    'name':flowVars.name,
                    'id':flowVars.id,
                    'designation':'Designation'
                   ]
      ]" doc:name="Expression"/>

         <json:object-to-json-transformer doc:name="Object to JSON"/>     
         <logger level="INFO" message="#[message.payload]" doc:name="Logger"/>
    </flow>

You can modify this flow as per your requirement, and you can generate the JSON you require in any format in very easy way



回答2:

If you are using the enterprise version of Mule, the answer is simple. Use a DataMapper. That is exactly what the DataMapper is designed to do, transform data. If you are using the community edition, then the answer really depends on how much you need to transform the data. I would start by transforming the json into a java.util.Map. That makes it easy to work with. Once you have it as a Map, then you can either use MEL expressions or write a custom java transformer to change the data into whatever format you need (still using a Map). Then it is trivial to transform the resulting Map back into Json.

<flow name="jsonToJson">
     <vm:inbound-endpoint exchange-pattern="request-response" path="jsonToJson" doc:name="VM"/>
     <json:json-to-object-transformer returnClass="java.util.Map" doc:name="JSON to Object"/>
     <custom-transformer class="com.example.myMapToMapDataTransformer" doc:name="Java"/>
     <json:object-to-json-transformer doc:name="Object to JSON"/>
</flow>