英文:
Merge Array object elements using comparison
问题
以下是翻译好的部分:
{
"data": [
{
"name": "ram",
"eno": "100",
"dept": "Sales",
"sal": 2000
},
{
"name": "gopal",
"eno": "101",
"dept": "Sales",
"sal": 2300
},
{
"name": "hari",
"eno": "102",
"dept": "Sales",
"sal": 1800
},
{
"name": "raju",
"eno": "104",
"dept": "Sales",
"sal": 2500
}
]
}
英文:
We have a requirement where we need to feed data into an empty json array element from another array object using element comparison.
The sample payloads and required results are mentioned below for better understanding.
Payload1: (Input Payload)
{
"data": [
{
"name":"ram",
"eno":"100",
"dept":"Sales",
"sal":null
},
{
"name":"gopal",
"eno":"101",
"dept":"Sales",
"sal":null
},
{
"name":"hari",
"eno":"102",
"dept":"Sales",
"sal":null
},
{
"name":"pankaj",
"eno":"103",
"dept":"Sales",
"sal":null
},
{
"name":"raju",
"eno":"104",
"dept":"Sales",
"sal":null
}
]
}
Payload2: (Response From a third party webservice)
{
"data": [
{
"eno": "100",
"sal": 2000
},
{
"eno": "101",
"sal": 2300
},
{
"eno": "102",
"sal": 1800
},
{
"eno": "104",
"sal": 2500
}
]
}
Required Result:
{
"data": [
{
"name":"ram",
"eno":"100",
"dept":"Sales",
"sal":2000
},
{
"name":"gopal",
"eno":"101",
"dept":"Sales",
"sal":2300
},
{
"name":"hari",
"eno":"102",
"dept":"Sales",
"sal":1800
},
{
"name":"raju",
"eno":"104",
"dept":"Sales",
"sal":2500
}
]
}
.................................................................
答案1
得分: 1
以下是使用Synapse的方法来完成此操作。我已经硬编码了第二个响应。
<?xml version="1.0" encoding="UTF-8"?>
<api context="/HelloWorld" name="HelloWorld" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<enrich>
<source clone="true" type="body"/>
<target property="inputBody" type="property"/>
</enrich>
<payloadFactory media-type="json">
<format>
{
"data": [
{
"eno": "100",
"sal": 2000
},
{
"eno": "101",
"sal": 2300
},
{
"eno": "102",
"sal": 1800
},
{
"eno": "104",
"sal": 2500
}
]
}
</format>
<args/>
</payloadFactory>
<foreach expression="json-eval($.data)" id="foreach_1">
<sequence>
<property expression="json-eval($.eno)" name="eno" scope="default" type="STRING"/>
<property expression="json-eval($.sal)" name="sal" scope="default" type="STRING"/>
<enrich>
<source clone="true" property="inputBody" type="property"/>
<target type="body"/>
</enrich>
<enrich>
<source clone="true" property="sal" type="property"/>
<target xpath="//data[eno=$ctx:eno]/sal"/>
</enrich>
<enrich>
<source clone="true" type="body"/>
<target property="inputBody" type="property"/>
</enrich>
</sequence>
</foreach>
<enrich>
<source clone="true" property="inputBody" type="property"/>
<target type="body"/>
</enrich>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
希望这对您有帮助。
英文:
Here is how you can do this by only using synapse. I hardcoded the second response.
<?xml version="1.0" encoding="UTF-8"?>
<api context="/HelloWorld" name="HelloWorld" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<enrich>
<source clone="true" type="body"/>
<target property="inputBody" type="property"/>
</enrich>
<payloadFactory media-type="json">
<format>
{
"data": [
{
"eno": "100",
"sal": 2000
},
{
"eno": "101",
"sal": 2300
},
{
"eno": "102",
"sal": 1800
},
{
"eno": "104",
"sal": 2500
}
]
}
</format>
<args/>
</payloadFactory>
<foreach expression="json-eval($.data)" id="foreach_1">
<sequence>
<property expression="json-eval($.eno)" name="eno" scope="default" type="STRING"/>
<property expression="json-eval($.sal)" name="sal" scope="default" type="STRING"/>
<enrich>
<source clone="true" property="inputBody" type="property"/>
<target type="body"/>
</enrich>
<enrich>
<source clone="true" property="sal" type="property"/>
<target xpath="//data[eno=$ctx:eno]/sal"/>
</enrich>
<enrich>
<source clone="true" type="body"/>
<target property="inputBody" type="property"/>
</enrich>
</sequence>
</foreach>
<enrich>
<source clone="true" property="inputBody" type="property"/>
<target type="body"/>
</enrich>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论