如何使用 WSO2 XSLT 调解器将 JSON 转换为 JSON?

How to transform JSON to JSON using WSO2 XSLT mediator?

我在 WSO2 EI/ESB 版本 6.5.0 中使用 XSLT 中介。我需要将 json 转换为 json ,为此我想到了使用 XSLT 调解器。

输入json如下,

 {
                "claim_type": [
                    {
                        "value": "Buildings",
                        "code": 1,
                        "effectiveDate": "1920-01-01T00:00:00Z",
                        "system": "POLICY"
                    }
                  ]
                }

输出json如下,'value'会转化为'description','code'会转化为'id'

 "claim_type": [
                {
                    "description": "Buildings",
                    "id": 1,
                    
                }
              ]
            }
            

棘手的部分是'claim_type'不是固定的,它可以是任何文本'xxxxxxx'。我可以用 XSLT 代码来做吗?有人可以为此提出建议吗?

使用脚本中介的示例

<?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>