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.
You could use a
Once you have a map, just access the keys. Maybe something like
and
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.