如何使用 Dataweave 从 2 个不同的 json 消息创建 json 消息

How to create a json message from 2 different json message using Dataweave

我想从 2 条不同的 Json 消息中创建一条 json 消息。两个输入消息都来自流变量。如何使用 Dataweave 完成。

消息 1:

    [

     {
         "userId": 11,
         "name": "ABC",
         "age": 30
     },

     {
         "userId": 44,
         "name": "XYZ",
         "age": 30
     }

     ]

消息 2:

 [

 {
     "userId": 11,
     "Address": "BLR"
 },
 {
     "userId": 44,
     "Address": "CCU"
 }

 ]

预期输出:

[

 {
     "userId": 11,
     "name": "ABC",
     "Address": "BLR"
 },
 {
     "userId": 44,
     "name": "XYZ",
     "Address": "CCU"
 }

 ]

提前谢谢你, 尼特什

参考 this 文章。似乎是您用例的完美解决方案。

您可以使用查找而不是过滤器,因为使用过滤器会产生性能开销。 试试这个

%dw 1.0
%output application/json
 %var adressLookup = {(  flowVars.data map  {
    ($.userId ++ "") : $
 })}
 ---
payload map {
    userId : $.userId,
    name : $.name,
    Address : adressLookup[$.userId ++ ""].Address
}

其中有效载荷是 message1,flowVars.data 是 message2

P.S :我使用 $.userId ++ "" 作为 hashmap 的字符串格式的键,一些数字键如何不适用于 weave hashmap。

另请参考

希望对您有所帮助。