2

I have two input payloads and I want to combine a JSON object inside another nested JSON object to a specific object. The expected output is to combine Input 2 with Input 1 under "Response_Data".

I tried but I could combine Input 2 at the end but not able to add it to the "Response_Data".

Can someone help me write the dataweave code?

Input 1:

[{
    "Org_Response": {
        "Request_Criteria": {
            "Org_Type_Reference": {
                "ID": {
                    "type": "Org_Type",
                    "text": "Business_Unit"
                }
            },
            "Include_Inactive": "0"
        },
        **"Response_Data"**: {
            "Org": {
                "Reference": {
                    "ID": {
                        "type": "Business_Unit_Reference_ID",
                        "text": "999-99-FD"
                    }
                },
                "Org_Data": {
                    "Reference_ID": "999-99-FD",
                    "Name": "Management"
                }
            }
        }
    }
}]

Input 2:

{
    "Org": {
        "Reference": {
            "ID": {
                "type": "Business_Unit_Reference_ID",
                "text": "90000"
            }
        },
        "Org_Data": {
            "Reference_ID": "90000",
            "Name": "TAXES"
        }
    }
}

Expected Output:

[{
    "Org_Response": {
        "Request_Criteria": {
            "Org_Type_Reference": {
                "ID": {
                    "type": "Org_Type",
                    "text": "Business_Unit"
                }
            },
            "Include_Inactive": "0"
        },
        **"Response_Data"**: {
            "Org": {
                "Reference": {
                    "ID": {
                        "type": "Business_Unit_Reference_ID",
                        "text": "999-99-FD"
                    }
                },
                "Org_Data": {
                    "Reference_ID": "999-99-FD",
                    "Name": "Management"
                }
            },
            "Org": {
                "Reference": {
                    "ID": {
                        "type": "Business_Unit_Reference_ID",
                        "text": "90000"
                    }
                },
                "Org_Data": {
                    "Reference_ID": "90000",
                    "Name": "TAXES"
                }
            }
        }
    }
}]

Thanks in advance

user12277274
  • 105
  • 2
  • 15

2 Answers2

0

There are 2 options to do this but it is better to iterate through the first input and then add second input accordingly but you can try the below for time being -

First DW -

%dw 2.0
output application/json
var input1=[{
    "Org_Response": {
        "Request_Criteria": {
            "Org_Type_Reference": {
                "ID": {
                    "type": "Org_Type",
                    "text": "Business_Unit"
                }
            },
            "Include_Inactive": "0"
        },
        "Response_Filter": {
            "Page": "1",
            "Count": "999"
        },
        "**Response_Data**": {
            "Org": {
                "Reference": {
                    "ID": {
                        "type": "Business_Unit_Reference_ID",
                        "text": "999-99-FD"
                    }
                },
                "Org_Data": {
                    "Reference_ID": "999-99-FD",
                    "Name": "Management"
                }
            }
        }
    }
}]
var input2= [{
    "Org": {
        "Reference": {
            "ID": {
                "type": "Business_Unit_Reference_ID",
                "text": "90000"
            }
        },
        "Org_Data": {
            "Reference_ID": "90000",
            "Name": "TAXES"
        }
    }
}]
---
Org_Response : 
([input1[0].Org_Response - "**Response_Data**"] ++
(input1.Org_Response."**Response_Data**" ++ input2))

Second DW -

%dw 2.0
output application/json
var input1=[{
    "Org_Response": {
        "Request_Criteria": {
            "Org_Type_Reference": {
                "ID": {
                    "type": "Org_Type",
                    "text": "Business_Unit"
                }
            },
            "Include_Inactive": "0"
        },
        "Response_Filter": {
            "Page": "1",
            "Count": "999"
        },
        "**Response_Data**": {
            "Org": {
                "Reference": {
                    "ID": {
                        "type": "Business_Unit_Reference_ID",
                        "text": "999-99-FD"
                    }
                },
                "Org_Data": {
                    "Reference_ID": "999-99-FD",
                    "Name": "Management"
                }
            }
        }
    }
}]
var input2= [{
    "Org": {
        "Reference": {
            "ID": {
                "type": "Business_Unit_Reference_ID",
                "text": "90000"
            }
        },
        "Org_Data": {
            "Reference_ID": "90000",
            "Name": "TAXES"
        }
    }
}]
---
Org_Response : input1.Org_Response map {
($ mapObject ((value, key, index) ->
{
     ((key) : value) if (key as String != "**Response_Data**"),
    ((key) : [value] + input2) if (key as String == "**Response_Data**")
}
 ))
}
0

Script

%dw 2.0
output application/json
var inp2= {
    "Org": {
        "Reference": {
            "ID": {
                "type": "Business_Unit_Reference_ID",
                "text": "90000"
            }
        },
        "Org_Data": {
            "Reference_ID": "90000",
            "Name": "TAXES"
        }
    }
}
var respData = payload.Org_Response."**Response_Data**"
var respDataNew = respData + inp2
---
[Org_Response: payload[0].Org_Response - "**Response_Data**" ++ 
{
    "**Response_Data**": respDataNew
}]
Salim Khan
  • 4,233
  • 11
  • 15