简体   繁体   中英

Mule ESB: Converting JSON Object to another object

I have the following flow:

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

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" 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:spring="http://www.springframework.org/schema/beans" version="EE-3.5.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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/db http://www.mulesoft.org/schema/mule/db/current/mule-db.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/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <db:generic-config name="Generic_Database_Configuration" url="jdbc:db2://localhost:50000/TEST:user=instuid;password=instpw;" driverClassName="com.ibm.db2.jcc.DB2Driver" doc:name="Generic Database Configuration"/>
    <flow name="test2Flow1" doc:name="test2Flow1">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
        <db:select config-ref="Generic_Database_Configuration" doc:name="Database" doc:description="test">
            <db:parameterized-query><![CDATA[SELECT SUM(BAL) FROM xxxx.ACCT]]></db:parameterized-query>
        </db:select>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
    </flow>
</mule>

The flow words well, and as you can see the JSON object converts the Database object to the following: (A JSON arry with a single object):

[{"1":444}]

Notice that the 444 is a numeric value (it does not have quotes around it).

What I want to do

  • Create a simple JSON structure without an array

  • Change the 444 from a numeric value to a string value

  • Make it look something like: (placing the 444 into another structure)

    { "Total" : "444", "Date" : "14/07/14" }

I know that to get the system date, I perform the following:

#[server.dateTime.format('dd/MM/yy')]

... and I know to get the 444 value from the original string, I performed the following:

$..1

But I do not know what to do next.

Now that I used a JSON object to view the results of the database connector, what object do I do next to create my new structure.

Do I use another JSON object, how would I structure the expression ?

To write all numbers as strings you can set it on the Jackson object mapper and reference that custom object mapper from your transformer:

     <spring:beans>
        <spring:bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />

        <spring:bean
            class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
            <spring:property name="targetObject" ref="jacksonObjectMapper" />
            <spring:property name="targetMethod" value="configure" />
            <spring:property name="arguments">
                <spring:list>
                    <spring:value>WRITE_NUMBERS_AS_STRINGS</spring:value>
                    <spring:value>true</spring:value>
                </spring:list>
            </spring:property>
        </spring:bean>
    </spring:beans>


    <flow name="flow1" doc:name="flow1">
        ... 
        <json:object-to-json-transformer mapper-ref="jacksonObjectMapper" />
        ...
    </flow>

However this will dot it for all number fields. You might have to write a custom serializer for specific behaviour.

As for unwrapping the array. Not sure you can do this via the object mapper with the jackson version mule uses. But for this case, if your always just getting one result back from the query - you could possibly just unfold the array before the json transformer

<set-payload value="#[payload[0]]">
<json:object-to-json-transformer mapper-ref="jacksonObjectMapper" />

A simple way of doing is to use Mule's Expression Transformer .
You need to extract the values from your JSON array and store it into some variables like the following :-

<json:json-to-object-transformer returnClass="java.util.List" doc:name="JSON to Object" />
<set-variable variableName="Total" value="#[message.payload[0].1]"  doc:name="Variable" />

Now, your variable Total will contain the value 444

Next step is to store your Date into some other variable as follows :-

<set-variable variableName="Date" value="#[server.dateTime.format('dd/MM/yy')]" doc:name="Variable" />

Now, if these 2 steps are done, then you can create your required JSON in a very easy way using Expression Transformer as follows :-

<expression-transformer
            expression="#[[ 
                'Total': flowVars['Total'].toString(),
                'Date': flowVars['Date']
                        ]]" doc:name="Expression" />
<json:object-to-json-transformer doc:name="Object to JSON" />

This will produce and structure your required JSON :- {"Date":"12/08/15","Total":"444"} in a very easy way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM