简体   繁体   中英

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

I want to create a json message from 2 different Json message. Both input messages are coming from flow variables. How can it be done using Dataweave.

Message 1:

    [

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

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

     ]

Message 2:

 [

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

 ]

Expected output:

[

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

 ]

Thank You in advance, Nitesh

Refer this article. Seems the perfect solution for your usecase.

You can use lookup instead of filter as using filter is performance overhead. Try this

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

where payload is message1 and flowVars.data is message2

PS : I have used $.userId ++ "" as key in string format for hashmap, some how numeric key isn't work with weave hashmap.

also refer Merge two json payload with dataweave

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM