0

I am struggling of finding a feasible solution for my Azure Logic App. A HTTP Request-action call will list the virtual networks of an Azure subscription. The response look somehow like this:

{
    "value": [
        {
            "name": "virtualNetworkName1",
            "id": "/subscriptions/111-222-333/resourceGroups/resourceGroupName1/providers/Microsoft.Network/virtualNetworks/virtualNetworkName1",
            "etag": "W/\"11111-1111-111\"",
            "type": "Microsoft.Network/virtualNetworks",
            "location": "eastus",
            "properties": {
                "provisioningState": "Succeeded",
                "resourceGuid": "111-1111-11111",
                "addressSpace": {
                    "addressPrefixes": [
                        "192.168.0.0/25"
                    ]
                }
            }
        },
        {
            "name": "virtualNetworkName2",
            "id": "/subscriptions/111-222-333/resourceGroups/resourceGroupName2/providers/Microsoft.Network/virtualNetworks/virtualNetworkName2",
            "etag": "W/\"22222-2222-222\"",
            "type": "Microsoft.Network/virtualNetworks",
            "location": "westeurope",
            "properties": {
                "provisioningState": "Succeeded",
                "resourceGuid": "222-2222-22222",
                "addressSpace": {
                    "addressPrefixes": [
                        "192.168.1.0/24"
                    ]
                }
            }
        }
    ]
}

The resonse has even more properties which aren't necessary. Regarding to this, I would like to use the HTTP Response-action in a JSON format with only a few properties:

  • Name
  • Id
  • Location

Like this:

[
    {
        "name": "virtualNetworkName1",
        "id": "/subscriptions/111-222-333/resourceGroups/resourceGroupName1/providers/Microsoft.Network/virtualNetworks/virtualNetworkName1",
        "location": "eastus"
    },
    {
        "name": "virtualNetworkName2",
        "id": "/subscriptions/111-222-333/resourceGroups/resourceGroupName2/providers/Microsoft.Network/virtualNetworks/virtualNetworkName2",
        "location": "westeurope"
    }
]

Is it possible to realize it with only Logic App native actions?

kaiaschulz
  • 35
  • 1
  • 6
  • You can make use of the Parse JSON connector under Data Operations and set up the schema to only use those three fields in the array. – Brian Smith Feb 17 '23 at 19:06
  • Hey @BrianSmith, i already did this. Now, I can re-use the defined variables of the Parse JSON. What should I do with these to get the results as shown above of another JSON with only a few properties? – kaiaschulz Feb 20 '23 at 07:02

1 Answers1

1

I have reproduced in my environment and got expected results as below:

Firstly, I have initialized your output in a variable and then used parse Json and compose to get required output:

enter image description here

Parse Json Schema:

{
    "properties": {
        "value": {
            "items": {
                "properties": {
                    "id": {
                        "type": "string"
                    },
                    "location": {
                        "type": "string"
                    },
                    "name": {
                        "type": "string"
                    }
                },
                "type": "object"
            },
            "type": "array"
        }
    },
    "type": "object"
}

Output:

enter image description here

enter image description here

First json Output:

enter image description here

Second one:

enter image description here

Code view of Logic app:

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "Compose": {
                "inputs": {
                    "value": [
                        {
                            "etag": "W/\"11111-1111-111\"",
                            "id": "/subscriptions/111-222-333/resourceGroups/resourceGroupName1/providers/Microsoft.Network/virtualNetworks/virtualNetworkName1",
                            "location": "eastus",
                            "name": "virtualNetworkName1",
                            "properties": {
                                "addressSpace": {
                                    "addressPrefixes": [
                                        "192.168.0.0/25"
                                    ]
                                },
                                "provisioningState": "Succeeded",
                                "resourceGuid": "111-1111-11111"
                            },
                            "type": "Microsoft.Network/virtualNetworks"
                        },
                        {
                            "etag": "W/\"22222-2222-222\"",
                            "id": "/subscriptions/111-222-333/resourceGroups/resourceGroupName2/providers/Microsoft.Network/virtualNetworks/virtualNetworkName2",
                            "location": "westeurope",
                            "name": "virtualNetworkName2",
                            "properties": {
                                "addressSpace": {
                                    "addressPrefixes": [
                                        "192.168.1.0/24"
                                    ]
                                },
                                "provisioningState": "Succeeded",
                                "resourceGuid": "222-2222-22222"
                            },
                            "type": "Microsoft.Network/virtualNetworks"
                        }
                    ]
                },
                "runAfter": {},
                "type": "Compose"
            },
            "For_each": {
                "actions": {
                    "Compose_2": {
                        "inputs": {
                            "id": "@items('For_each')?['id']",
                            "location": "@items('For_each')?['location']",
                            "name": "@items('For_each')?['name']"
                        },
                        "runAfter": {},
                        "type": "Compose"
                    }
                },
                "foreach": "@body('Parse_JSON')?['value']",
                "runAfter": {
                    "Parse_JSON": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Parse_JSON": {
                "inputs": {
                    "content": "@outputs('Compose')",
                    "schema": {
                        "properties": {
                            "value": {
                                "items": {
                                    "properties": {
                                        "id": {
                                            "type": "string"
                                        },
                                        "location": {
                                            "type": "string"
                                        },
                                        "name": {
                                            "type": "string"
                                        }
                                    },
                                    "type": "object"
                                },
                                "type": "array"
                            }
                        },
                        "type": "object"
                    }
                },
                "runAfter": {
                    "Compose": [
                        "Succeeded"
                    ]
                },
                "type": "ParseJson"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {}
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "parameters": {}
}

Now you will get required fields as I have got.

RithwikBojja
  • 5,069
  • 2
  • 3
  • 7
  • Thanks a lot for the detailed explanation. I guess the problem is the handling of the outcome of the compose. I would like to respond in one "HTTPS Response"-action as shown in the initial post. I reached the same result as you, but here I am lost. In your result you are composing each item. – kaiaschulz Feb 20 '23 at 07:01
  • 1
    You can add the compose iterations into one json You can use [Union](https://stackoverflow.com/questions/68193877/how-do-i-merge-multiple-compose-objects-to-a-json-array) – RithwikBojja Feb 20 '23 at 07:03
  • 1
    You can add by taking each item of iteration. – RithwikBojja Feb 20 '23 at 07:13
  • 1
    I found a solution. I am using in the foreach-loop a `Append to array variable`-action were I am using a `concat` to build the inner JSON format `concat('{"Id":"',body('Parse_JSON_-_Current_item')?['id'],'","Name":"',body('Parse_JSON_-_Current_item')?['name'],'","Location":"',body('Parse_JSON_-_Current_item')?['location'],'"}')`. On the outside of the foreach-loop I used another `Compose`-action with the mentioned `join` on the array `join(variables('arrVirtualNetworks'),',')` + a `Compose`-action for the outer JSON format `concat('{"ListVirtualNetworks": [',outputs('Compose_-_Join'),']}')`. – kaiaschulz Feb 20 '23 at 16:21