【问题标题】:How to transform JSON to JSON using WSO2 XSLT mediator?如何使用 WSO2 XSLT 中介将 JSON 转换为 JSON?
【发布时间】:2020-07-01 15:26:51
【问题描述】:

我在 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 代码来做吗?有人可以为此提出建议吗?

【问题讨论】:

  • (1) 您应该将尝试显示为minimal reproducible example,并询问您遇到的特定编程僵局。 (2) 如果不涉及 XML,为什么使用 XSLT 而不是 JavaScript 进行 JSON 到 JSON 的映射?
  • 感谢您的回复。我正在使用接受 XSLT 作为转换中介的工具。因此为此寻找 XSLT 解决方案
  • edit 你的问题解决我上面的第 (1) 点,并包括工具的名称,尤其是它支持的最新版本的 XSLT。谢谢。
  • XSLT 3 支持json-to-xmlxml-to-json,当然它也可以转换中间XML 格式,参见xsltfiddle.liberty-development.net/pNmCzsL。但是wso2.com/products/enterprise-service-bus 的文档只提到了 XSLT 2,因此您必须检查您的平台是否存在任何专有函数来将 JSON 转换为 XSLT 2 然后可以转换的某些 XML,以及将 XML 转换回 JSON 的专有函数。跨度>
  • 如果您发布工具从输入中生成的 XML,以及工具期望生成所需 JSON 结果的 XML,您可能会获得更多帮助。

标签: json xml xslt wso2esb


【解决方案1】:

使用脚本中介的示例

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

【讨论】:

  • 非常感谢,看来这对我有用
猜你喜欢
  • 2016-12-26
  • 2021-05-06
  • 2020-12-01
  • 2012-10-12
  • 2021-07-22
  • 1970-01-01
  • 2021-01-24
  • 2020-01-26
相关资源
最近更新 更多