将两个json负载与dataweave合并 [英] Merge two json payload with dataweave

查看:98
本文介绍了将两个json负载与dataweave合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Mule流,它调用两个REST API的结果,从而产生两个json列表有效负载.我需要从这两个有效负载中获取一个合并的json列表.

I have a Mule flow that calls two REST API's resulting in two json list payloads. I need to obtain a merged json list from these two payloads.

我设法通过使用自定义聚合器策略的分散聚集流实现了这一目标.但是,我为此编写的代码看起来很杂乱,而且速度很慢.

I managed to achieve this with a scatter-gather flow with a custom aggregator strategy. However the code I had to write for this looks rather messy and it's slow.

有什么方法可以使用dataweave来实现?"

Is there any way to achieve this using dataweave?`

输入Json清单1:

[{ "ID": "1", "firstName": "JANE""familyName": "DOE", "Entities": [{ "ID": "515785", "name": "UNKOWN UNITED", "callSign": "UU" }] }, { "ID": "2", "firstName": "JOHN", "familyName": "DOE", "Entities": [{ "Entities_ID": "515785", "name": "UNKOWN UNITED", "callSign": "UU" }] }]

[{ "ID": "1", "firstName": "JANE""familyName": "DOE", "Entities": [{ "ID": "515785", "name": "UNKOWN UNITED", "callSign": "UU" }] }, { "ID": "2", "firstName": "JOHN", "familyName": "DOE", "Entities": [{ "Entities_ID": "515785", "name": "UNKOWN UNITED", "callSign": "UU" }] }]

输入Json清单2:

[{ "ID": "1", "firstName": "JANE", "familyName": "DOE", "Entities": [{ "Entities_ID": "8916514", "name": "UNKOWN INCORPORATED", "callSign": "UI" }] }, { "ID": "4", "firstName": "JAKE", "familyName": "DOE", "Entities": [{ "Entities_ID": "8916514", "name": "UNKOWN INCORPORATED", "callSign": "UI" }] }]

[{ "ID": "1", "firstName": "JANE", "familyName": "DOE", "Entities": [{ "Entities_ID": "8916514", "name": "UNKOWN INCORPORATED", "callSign": "UI" }] }, { "ID": "4", "firstName": "JAKE", "familyName": "DOE", "Entities": [{ "Entities_ID": "8916514", "name": "UNKOWN INCORPORATED", "callSign": "UI" }] }]

所需的输出是没有重复ID的合并列表:

desired output is a merged list without duplicate IDs:

[{ "ID": "1", "firstName": "JANE", "familyName": "DOE", "Entities": [{ "Entities_ID": "515785", "name": "UNKOWN UNITED", "callSign": "UU" }, { "Entities_ID": "8916514", "name": "UNKOWN INCORPORATED", "callSign": "UI" }] }, { "ID": "2", "firstName": "JOHN", "familyName": "DOE", "Entities": [{ "Entities_ID": "515785", "name": "UNKOWN UNITED", "callSign": "UU" }] }, { "ID": "4", "firstName": "JAKE", "familyName": "DOE", "Entities": [{ "Entities_ID": "8916514", "name": "UNKOWN INCORPORATED", "callSign": "UI" }] }]

[{ "ID": "1", "firstName": "JANE", "familyName": "DOE", "Entities": [{ "Entities_ID": "515785", "name": "UNKOWN UNITED", "callSign": "UU" }, { "Entities_ID": "8916514", "name": "UNKOWN INCORPORATED", "callSign": "UI" }] }, { "ID": "2", "firstName": "JOHN", "familyName": "DOE", "Entities": [{ "Entities_ID": "515785", "name": "UNKOWN UNITED", "callSign": "UU" }] }, { "ID": "4", "firstName": "JAKE", "familyName": "DOE", "Entities": [{ "Entities_ID": "8916514", "name": "UNKOWN INCORPORATED", "callSign": "UI" }] }]

推荐答案

这对我来说适用于dataweave,其中有效负载为list1,flowVars.list2为list2

This works for me with dataweave where payload is list1 and flowVars.list2 is list2

%dw 1.0
%output application/json
%var mergeddata = flowVars.list2 groupBy $.ID
---
payload map ((data,index) -> {
   ID: data.ID,
   firstName : data.firstName,
   familyName : data.familyName,
   Entities : data.Entities ++ (mergeddata[data.ID].Entities default [])
}) ++   
(flowVars.list2 filter (not (payload.ID contains $.ID)))

希望这会有所帮助

这篇关于将两个json负载与dataweave合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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