json - Mule Server 3.6 > Anypoint Studio > Data Extraction -


how can extract information against json data, when have sessionvars.filters containing:

["account", "billing"] 

...where 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, able return following payload (using 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 trying in anypoint studio:

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

from "pseudo-code" may able create final response payload 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 mention sessionvars.filters array dynamic depending on request can contain ["billing"] only, or ["product", "account"], or etc.

you use

<transformer ref="stringtoobject" returnclass="java.util.hashmap"/> 

once have map, access keys. maybe like

#[payload.billing] 

and

#[payload.account] 

basically json path little limited in mule, rather transform json hashmap , query them either using mel or programatically.

addendum

below dummy flow uses script transformer filter out un required fields payload, implementation of 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 json"/>         <logger message="final payload #[payload]" level="info" doc:name="logger"/>     </flow> 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -