Search code examples
azure-logic-appsazure-logic-app-standard

How to assign JSON field from one data source to another in Logic App


I have two different JSON data coming into my Logic App:

Data 1:

[
   {
      "EmployeeCode":"123",
      "Username":"abc"
   },
   {
      "EmployeeCode":"456",
      "Username":"def"
   }
]

Data 2:

[
   {
      "EmployeeCode":"123",
      "Team":"IT"
   },
   {
      "EmployeeCode":"456",
      "Team":"Finance"
   }
]

And I want to generate final output like this:

Final output:

[
   {
      "EmployeeCode":"123",
      "Username":"abc",
      "Team":"IT"
   },
   {
      "EmployeeCode":"456",
      "Username":"def",
      "Team":"Finance"
   }
]

Is there a simple way to achieve this in Logic App itself? Without using JavaScript or Azure Function or anything?


Solution

  • After reproducing from our end here is how we could able to achieve your requirement.

    First, we have used 2 Parse JSON for each data to extract the items in the JSON.

    enter image description here

    then used a condition connector to compare the EmployeeCode, and then merged using compose connector.

    enter image description here

    However to make the whole JSON to be used for future purposes we have initialized an array variable and then appended the successful runs from the condition connector. Here is my logic app.

    enter image description here

    RESULTS:

    enter image description here

    Below is the code view of my logic app

    {
        "definition": {
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
            "actions": {
                "FinalJSON": {
                    "inputs": "@variables('FinalJson')",
                    "runAfter": {
                        "For_each": [
                            "Succeeded"
                        ]
                    },
                    "type": "Compose"
                },
                "For_each": {
                    "actions": {
                        "For_each_2": {
                            "actions": {
                                "Condition": {
                                    "actions": {
                                        "Append_to_array_variable": {
                                            "inputs": {
                                                "name": "FinalJson",
                                                "value": "@outputs('Compose')"
                                            },
                                            "runAfter": {
                                                "Compose": [
                                                    "Succeeded"
                                                ]
                                            },
                                            "type": "AppendToArrayVariable"
                                        },
                                        "Compose": {
                                            "inputs": {
                                                "EmployeeCode": "@{items('For_each')['EmployeeCode']}",
                                                "Team": "@{items('For_each_2')['Team']}",
                                                "Username": "@{items('For_each')['Username']}"
                                            },
                                            "runAfter": {},
                                            "type": "Compose"
                                        }
                                    },
                                    "expression": {
                                        "and": [
                                            {
                                                "equals": [
                                                    "@items('For_each')['EmployeeCode']",
                                                    "@items('For_each_2')['EmployeeCode']"
                                                ]
                                            }
                                        ]
                                    },
                                    "runAfter": {},
                                    "type": "If"
                                }
                            },
                            "foreach": "@body('Parse_JSON2')",
                            "runAfter": {},
                            "type": "Foreach"
                        }
                    },
                    "foreach": "@body('Parse_JSON1')",
                    "runAfter": {
                        "Initialize_variable": [
                            "Succeeded"
                        ]
                    },
                    "type": "Foreach"
                },
                "Initialize_variable": {
                    "inputs": {
                        "variables": [
                            {
                                "name": "FinalJson",
                                "type": "array"
                            }
                        ]
                    },
                    "runAfter": {
                        "Parse_JSON2": [
                            "Succeeded"
                        ]
                    },
                    "type": "InitializeVariable"
                },
                "JSON1": {
                    "inputs": [
                        {
                            "EmployeeCode": "123",
                            "Username": "abc"
                        },
                        {
                            "EmployeeCode": "456",
                            "Username": "def"
                        }
                    ],
                    "runAfter": {},
                    "type": "Compose"
                },
                "JSON2": {
                    "inputs": [
                        {
                            "EmployeeCode": "123",
                            "Team": "IT"
                        },
                        {
                            "EmployeeCode": "456",
                            "Team": "Finance"
                        }
                    ],
                    "runAfter": {
                        "JSON1": [
                            "Succeeded"
                        ]
                    },
                    "type": "Compose"
                },
                "Parse_JSON1": {
                    "inputs": {
                        "content": "@outputs('JSON1')",
                        "schema": {
                            "items": {
                                "properties": {
                                    "EmployeeCode": {
                                        "type": "string"
                                    },
                                    "Username": {
                                        "type": "string"
                                    }
                                },
                                "required": [
                                    "EmployeeCode",
                                    "Username"
                                ],
                                "type": "object"
                            },
                            "type": "array"
                        }
                    },
                    "runAfter": {
                        "JSON2": [
                            "Succeeded"
                        ]
                    },
                    "type": "ParseJson"
                },
                "Parse_JSON2": {
                    "inputs": {
                        "content": "@outputs('JSON2')",
                        "schema": {
                            "items": {
                                "properties": {
                                    "EmployeeCode": {
                                        "type": "string"
                                    },
                                    "Team": {
                                        "type": "string"
                                    }
                                },
                                "required": [
                                    "EmployeeCode",
                                    "Team"
                                ],
                                "type": "object"
                            },
                            "type": "array"
                        }
                    },
                    "runAfter": {
                        "Parse_JSON1": [
                            "Succeeded"
                        ]
                    },
                    "type": "ParseJson"
                }
            },
            "contentVersion": "1.0.0.0",
            "outputs": {},
            "parameters": {},
            "triggers": {
                "manual": {
                    "inputs": {
                        "schema": {}
                    },
                    "kind": "Http",
                    "type": "Request"
                }
            }
        },
        "parameters": {}
    }