合并数组对象元素使用比较。

huangapple go评论54阅读模式
英文:

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>

huangapple
  • 本文由 发表于 2023年2月8日 18:28:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384426.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定