如何基于Mule中的键值合并两个Json [英] how to merge two Json based on key value in Mule

查看:137
本文介绍了如何基于Mule中的键值合并两个Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的json

Hi I have a 1 json as below

"first": [
{
  "projectid": "15",
  "approval_status": "A"
},
{
  "projectid": "24",
  "approval_status": "A"
}  ]}

下一个有效负载存储在flowVariable

The next payload is stores in a flowVariable

{
 "Second": [
{
  "projectid": "15",
  "total": "123",
  "updated": "yes"
},
   {
  "projectid": "24",
  "total": "123",
  "updated": "yes"
}]}

我正在使用datawevae合并这些有效负载,但没有给出预期结果,我的预期值为

Am using datawevae to merge these payload but its not giving expected result my expected value is

 {
 "Result": [
{
  "projectid": "15",
  "total": "123",
  "approval_status": "A"
},
   {
  "projectid": "24",
  "total": "123",
  "approval_status": "A"
}]}

推荐答案

我不确定您如何同时获得两个有效载荷

I am not sure how are you getting both the payloads at the same time

但是,如果您既能够获得有效载荷又能够保存为2个流量变量,则可以使用表达式转换器来构造组合的有效载荷.
下面是一个示例,您可以尝试将两个有效负载动态组合:-

But if you are able to get both the payload and able to save into 2 flow variables, then you can use an Expression transformer to construct your combined payload.
Below is a sample you can try to combine both the payload dynamically:-

     <set-payload value="{
    &quot;first&quot;: [{
        &quot;projectid&quot;: &quot;15&quot;,
        &quot;approval_status&quot;: &quot;A&quot;
    }, {
        &quot;projectid&quot;: &quot;24&quot;,
        &quot;approval_status&quot;: &quot;A&quot;
    }]
   }" mimeType="application/json" doc:name="Set Payload"/>
       <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
        <set-variable variableName="var1" value="#[message.payload]" doc:name="Variable"/>

        <set-payload value="{
    &quot;Second&quot;: [{
        &quot;projectid&quot;: &quot;15&quot;,
        &quot;total&quot;: &quot;123&quot;,
        &quot;updated&quot;: &quot;yes&quot;
    }, {
        &quot;projectid&quot;: &quot;24&quot;,
        &quot;total&quot;: &quot;123&quot;,
        &quot;updated&quot;: &quot;yes&quot;
    }]
  }" mimeType="application/json" doc:name="Set Payload"/>
        <json:json-to-object-transformer returnClass="java.util.HashMap" doc:name="JSON to Object"/>
        <set-variable variableName="var2" value="#[message.payload]" doc:name="Variable"/>

        <expression-transformer
     expression="#[[

     'Result':[
     {
      'projectid': flowVars.var1.first[0].projectid,
      'total': flowVars.var2.Second[0].total,
      'approval_status': flowVars.var1.first[0].approval_status
    },
    {
     'projectid': flowVars.var1.first[1].projectid,
     'total': flowVars.var2.Second[1].total,
     'approval_status': flowVars.var1.first[1].approval_status
   }

  ]]
      ]" doc:name="Expression"/>
  <json:object-to-json-transformer doc:name="Object to JSON"/>     

请注意,上面的示例只是为了展示如何动态地动态快速组合有效负载.
在这里,您需要将有效负载都存储在流变量 var1 var2 (I have used set payload just to demonstrate, you need to get it dynamically)中,然后可以动态构造有效负载使用表达式Transformer
还请注意,如果您要以完全相同的结构获取JSON有效负载,则可以使用以上代码对有效负载进行快速组合...
但是,如果您的json结构发生更改,则说您的有效负载中还包含一个列表元素,那么您需要修改上述代码,并使用for循环来获取值.

Please note, the above example is just to show how you can combine the payload dynamically and quickly.
Here you need to store both the payload in a flow variable var1 and var2, (I have used set payload just to demonstrate, you need to get it dynamically) , then you can construct the payload dynamically using the expression transformer
Also note, if you are getting the json payload in the exact same structure, you can use the above code for a quick combination of payloads...
But, if your json structure changes say one more list element included in your payload, then you need to modify the above code and use a for loop to get the values.

上面的代码将产生一个快速组合的有效载荷,如下所示:-

The above code will produce a quick combined payload as follows:-

{
  "Result": [
    {
      "total": "123",
      "projectid": "15",
      "approval_status": "A"
    },
    {
      "total": "123",
      "projectid": "24",
      "approval_status": "A"
    }
  ]
}

这篇关于如何基于Mule中的键值合并两个Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆