简体   繁体   English

如何使用 WSO2 Z139E293A7146F23CD247C6BE04 介质将 JSON 转换为 JSON?

[英]How to transform JSON to JSON using WSO2 XSLT mediator?

I was using XSLT mediator inside WSO2 EI/ESB version 6.5.0.我在 WSO2 EI/ESB 版本 6.5.0 中使用 XSLT 调解器。 I need to transform json to json, for that i thought of using XSLT mediator.我需要将 json 转换为 json,为此我想到了使用 XSLT 介体。

Input json as follows,输入json如下,

 {
                "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' Output json 如下,'value'将转换为'description','code'转换为'id'

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

Tricky part is 'claim_type' is not fixed, it can be any text'xxxxxxx'.棘手的部分是'claim_type' 不固定,它可以是任何文本'xxxxxxx'。 Can i do it with XSLT code.我可以用 XSLT 代码来做吗? 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM