Mule Server 3.6 > Anypoint Studio > Data Extraction

176 views Asked by At

How can extract information against a JSON data, when I have a sessionVars.filters containing:

["account", "billing"]

...where my JSON data contains:

{
    "billing": {
        "BillNumber": 25,
        "BillPeriod": "06 Dec 14 - 05 Jan 15",
        "AccountNumber": 78781843,
        "PreviousBalance": 0.00,
        "CurrentBalance": 1237.49,
        "DueDate": "Jan 26, 2015",
        "TotalAmountDue": 1237.49,
        "PreviousBalance": 0.00,
        "CurrentBalance": 1237.49,
        "DueDate": "Jan 26, 2015",
        "TotalAmountDue": "1237.49"
    },
    "product": ["hilly"],
    "account": {
        "Name": "Lee G. Ive",
        "Address": "214 Maya St., G2 Village Highlands City, Somewhere 1630"
    },
    "content": {
        "package": {
            "userId": "1234"
        },
        "service": {
            "username": "hershey123"
        }
    }
}

...from that, I would like to be able to return the following payload (using my filters array):

{
    "billing": {
        "BillNumber": 25,
        "BillPeriod": "06 Dec 14 - 05 Jan 15",
        "AccountNumber": 78781843,
        "PreviousBalance": 0.00,
        "CurrentBalance": 1237.49,
        "DueDate": "Jan 26, 2015",
        "TotalAmountDue": 1237.49,
        "PreviousBalance": 0.00,
        "CurrentBalance": 1237.49,
        "DueDate": "Jan 26, 2015",
        "TotalAmountDue": "1237.49"
    },
    "account": {
        "Name": "Lee G. Ive",
        "Address": "214 Maya St., G2 Village Highlands City, Somewhere 1630"
    }
}

Edit: Addendum

This is what I am trying to do in Anypoint Studio:

response = {
    "result": {
        "code": 200,
        "status": "success"
    }
};
foreach (sessionVars.filters as filter) {
    response[filter] = data[filter];
}

From that "pseudo-code" I may be able to create my final response payload to be:

{
    "result": {
        "code": 200,
        "status": "success"
    },
    "billing": {
        "BillNumber": 25,
        "BillPeriod": "06 Dec 14 - 05 Jan 15",
        "AccountNumber": 78781843,
        "PreviousBalance": 0.00,
        "CurrentBalance": 1237.49,
        "DueDate": "Jan 26, 2015",
        "TotalAmountDue": 1237.49,
        "PreviousBalance": 0.00,
        "CurrentBalance": 1237.49,
        "DueDate": "Jan 26, 2015",
        "TotalAmountDue": "1237.49"
    },
    "account": {
        "Name": "Lee G. Ive",
        "Address": "214 Maya St., G2 Village Highlands City, Somewhere 1630"
    }
}

I forgot to mention that the sessionVars.filters array is dynamic depending on the request which can contain ["billing"] only, or ["product", "account"], or etc.

1

There are 1 answers

4
Sudarshan On BEST ANSWER

You could use a

<transformer ref="StringToObject" returnClass="java.util.HashMap"/>

Once you have a map, just access the keys. Maybe something like

#[payload.billing]

and

#[payload.account]

Basically JSON path is a little limited in Mule, so you rather transform the JSON into a HashMap and query them either using MEL or programatically.

Addendum

Below is a dummy flow which uses a script transformer to filter out the un required fields from the payload, it is essentially the implementation of your logic.

<flow name="soapwsFlow2">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/transformer" doc:name="8081/transformer"/>
        <set-payload value="#[['accounts':'lots of accountingJson','billing':'even more billing json','somethingelse':'lots of other stuff']]" doc:name="Set Payload"/>
        <set-session-variable variableName="filters" value="#[['accounts','billing']]" doc:name="Session Variable"/>
        <scripting:transformer returnClass="java.util.HashMap" doc:name="Groovy Transformer">
            <scripting:script engine="Groovy"><![CDATA[Map payloadData = payload
Map filteredData = new HashMap();
for(String filter : sessionVars.filters){
    filteredData.put(filter,payloadData[filter]);
}
return filteredData]]></scripting:script>
        </scripting:transformer>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
        <logger message="FInal Payload #[payload]" level="INFO" doc:name="Logger"/>
    </flow>