json - 如何使用 WSO2 XSLT 中介将 JSON 转换为 JSON?
问题描述
我在 WSO2 EI/ESB 版本 6.5.0 中使用 XSLT 调解器。我需要将 json 转换为 json ,为此我想到了使用 XSLT mediator。
输入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>
推荐阅读
- android - ConstraintLayout AutoTransition 动画不起作用
- c - stdio 是否将文件描述符偏移设置回文件关闭时的下一个未读位置?
- vim - 当文本隐藏在 nowrap 下时,转到实际的行尾
- python - 链表结构中的迭代问题
- node.js - 防止快递中的默认响应
- objective-c - 简单的版本检查/比较来自 Web 的字符串
- java - 为什么只有抽象类可以有构造函数而接口不能,而我可以通过匿名方法创建两者的 obj
- c# - 使用 Windows 窗体初始化程序时,如何选择工具条中的哪个按钮被选中?
- c# - 二维数组中出现意外的超出范围的数组错误?
- spring - 如何将 openapi.yaml 规范公开到 rest spring boot