简体   繁体   中英

Merging 1 or more ODATA responses into 1 JSON message in Azure Logic Apps

I need some advise how to proceed on the following case.

I am creating a API and this API will call 1 or more SAP ODATA services. from these ODATA complex responses I want to create my own response and give this back to my API.

How to solve this requirement? I have been struggling whit some FOR EACH scenario's I found , but after deploying these example the Logic App hangs for some reason.

Thanks!

After reproducing from my end this works fine. I'll try to explain how I achieved your requirement. Below is the whole flow of my Logic app.

在此处输入图像描述

Consider below are the two responses that I'm receiving.

Input-1

{
   "firstName": "SampleFirstName",
   "lastName": "SampleLastName"
}

Input-2

{
   "email": "sample@sample.com",
   "Tasks": [
      {
         "TaskNo": "1",
         "Date": "2022-08-25"
      },
      {
         "TaskNo": "2",
         "Date": "2022-08-25"
      },
      {
         "TaskNo": "3",
         "Date": "2022-08-25"
      }
   ]
}

在此处输入图像描述

In the next step I'm trying to Parse both the inputs

在此处输入图像描述

Below is the JSON Schema for Parse JSON for Input-1

{
    "properties": {
        "firstName": {
            "type": "string"
        },
        "lastName": {
            "type": "string"
        }
    },
    "type": "object"
}

Below is the JSON Schema for Parse JSON for Input-2

{
    "properties": {
        "Tasks": {
            "items": {
                "properties": {
                    "Date": {
                        "type": "string"
                    },
                    "TaskNo": {
                        "type": "string"
                    }
                },
                "required": [
                    "TaskNo",
                    "Date"
                ],
                "type": "object"
            },
            "type": "array"
        },
        "email": {
            "type": "string"
        }
    },
    "type": "object"
}

You can generate your own schema using Use sample payload to generate schema for your Parse JSON action.

在此处输入图像描述

In the next step you can use Compose action to have your custom built JSON. Below is how I'm merging both the JSONs.

在此处输入图像描述

RESULTS:

在此处输入图像描述

Alternatively, you can map the objects using Select action

在此处输入图像描述

You can reproduce the same from your end using the below codeview.

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Input-1": {
                "inputs": {
                    "firstName": "SampleFirstName",
                    "lastName": "SampleLastName"
                },
                "runAfter": {},
                "type": "Compose"
            },
            "Input-2": {
                "inputs": {
                    "Tasks": [
                        {
                            "Date": "2022-08-25",
                            "TaskNo": "1"
                        },
                        {
                            "Date": "2022-08-25",
                            "TaskNo": "2"
                        },
                        {
                            "Date": "2022-08-25",
                            "TaskNo": "3"
                        }
                    ],
                    "email": "sample@sample.com"
                },
                "runAfter": {
                    "Input-1": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Merged_JSON": {
                "inputs": {
                    "Tasks": "@body('Parse_JSON_for_Input-2')?['Tasks']",
                    "firstName": "@{body('Parse_JSON_for_Input-1')?['firstName']}",
                    "lastName": "@{body('Parse_JSON_for_Input-1')?['lastName']}"
                },
                "runAfter": {
                    "Parse_JSON_for_Input-2": [
                        "Succeeded"
                    ]
                },
                "type": "Compose"
            },
            "Parse_JSON_for_Input-1": {
                "inputs": {
                    "content": "@outputs('Input-1')",
                    "schema": {
                        "properties": {
                            "firstName": {
                                "type": "string"
                            },
                            "lastName": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    }
                },
                "runAfter": {
                    "Input-2": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            },
            "Parse_JSON_for_Input-2": {
                "inputs": {
                    "content": "@outputs('Input-2')",
                    "schema": {
                        "properties": {
                            "Tasks": {
                                "items": {
                                    "properties": {
                                        "Date": {
                                            "type": "string"
                                        },
                                        "TaskNo": {
                                            "type": "string"
                                        }
                                    },
                                    "required": [
                                        "TaskNo",
                                        "Date"
                                    ],
                                    "type": "object"
                                },
                                "type": "array"
                            },
                            "email": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    }
                },
                "runAfter": {
                    "Parse_JSON_for_Input-1": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

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