I was using XSLT mediator inside WSO2 EI/ESB version 6.5.0. I need to transform json to json , for that i thought of using XSLT mediator .
Input json as follows ,
{ "claim_type": [ { "value": "Buildings", "code": 1, "effectiveDate": "1920-01-01T00:00:00Z", "system": "POLICY" } ] }
Output json as follows , ‘value’ will be transformed to ‘description’ and ‘code’ is transformed to ‘id’
"claim_type": [ { "description": "Buildings", "id": 1, } ] }
Tricky part is ‘claim_type’ is not fixed , it can be any text’xxxxxxx’ . Can i do it with XSLT code . Can some one please suggest hint for this ?
an example using a script mediator
<?xml version="1.0" encoding="UTF-8"?> <proxy xmlns="http://ws.apache.org/ns/synapse" name="transformation" startOnLoad="true" statistics="disable" trace="disable" transports="http,https,local"> <target> <inSequence> <payloadFactory media-type="json"> <format>{ "claim_type": [ { "value": "Buildings", "code": 1, "effectiveDate": "1920-01-01T00:00:00Z", "system": "POLICY" }, { "value": "Buildings2", "code": 2, "effectiveDate": "1920-01-01T00:00:00Z", "system": "POLICY2" } ] }</format> <args/> </payloadFactory> <script language="js"> var claims = mc.getPayloadJSON(); var log = mc.getServiceLog(); var keys = Object.keys(claims); claims = claims[keys[0]]; var response = {claims: []}; for(var i =0; i < claims.length; i++) { var item = claims[i]; response.claims.push({id: item.code, claim_type: item.value}); } mc.setPayloadJSON(response); </script> <log level="full"/> <property name="messageType" scope="axis2" type="STRING" value="application/json"/> <respond/> </inSequence> <outSequence/> <faultSequence/> </target> <description/> </proxy>