Search code examples
httprequestazure-logic-appskqlazure-resource-graph

Create an array from the output of POST request in JSON Object


I'm trying to send an email via Logic App. The content must be an array of an orphaned resources.

I am using an HTTP request to query the Azure resource graph explorer, on the Output I have a JSON object. I want to transform this JSON Object on an array.

enter image description here How to get an array from the output of HTTP POST request?


Solution

  • Here is how it worked for me:

    Taking provided JSON into consideration Instead of having body as From data in Create HTML table I have used data since the Create HTML table takes only array as input and then modified the same while sending the email.

    Here is my Logic app : enter image description here

    Here is the email : enter image description here

    Updated Answer

    Here is the whole logic app

    enter image description here

    For parse JSON I'm generating the schema using sample payload with the same JSON that you have provided with sample values. i.e..

    {
      "body": {
        "totalRecords": 25,
        "count": 25,
        "data": [
          {
            "id": "SampleId",
            "diskState": "Sample",
            "resourceGroup": "dkfdfjsi",
            "location": "gareg",
            "subscriptionId": "fgser"
          },
          {
            "id": "SampleID2",
            "diskState": "SAmple2",
            "resourceGroup": "dkfdfjsi",
            "location": "gareg",
            "subscriptionId": "fgser"
          }
        ]
      }
    }
    

    As your requirement is to send the data in a tabular format I have used the same connector as you did i.e. Create HTML Table with data array as input to the connector and so it will be generating the table automatically with the headers in the format mentioned below

    enter image description here

    Lastly, I have used the output of the HTML Table connector to send the email

    enter image description here

    Here 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": {
               "Create_HTML_table_2": {
                   "inputs": {
                       "format": "HTML",
                       "from": "@body('Parse_JSON')?['body']?['data']"
                   },
                   "runAfter": {
                       "Parse_JSON": [
                           "Succeeded"
                       ]
                   },
                   "type": "Table"
               },
               "Parse_JSON": {
                   "inputs": {
                       "content": "@triggerBody()",
                       "schema": {
                           "properties": {
                               "body": {
                                   "properties": {
                                       "count": {
                                           "type": "integer"
                                       },
                                       "data": {
                                           "items": {
                                               "properties": {
                                                   "diskState": {
                                                       "type": "string"
                                                   },
                                                   "id": {
                                                       "type": "string"
                                                   },
                                                   "location": {
                                                       "type": "string"
                                                   },
                                                   "resourceGroup": {
                                                       "type": "string"
                                                   },
                                                   "subscriptionId": {
                                                       "type": "string"
                                                   }
                                               },
                                               "required": [
                                                   "id",
                                                   "diskState",
                                                   "resourceGroup",
                                                   "location",
                                                   "subscriptionId"
                                               ],
                                               "type": "object"
                                           },
                                           "type": "array"
                                       },
                                       "totalRecords": {
                                           "type": "integer"
                                       }
                                   },
                                   "type": "object"
                               }
                           },
                           "type": "object"
                       }
                   },
                   "runAfter": {},
                   "type": "ParseJson"
               },
               "Send_an_email_(V2)_2": {
                   "inputs": {
                       "body": {
                           "Body": "<p><strong>Total Records</strong> :@{body('Parse_JSON')?['body']?['totalRecords']} , &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>Count </strong>: @{body('Parse_JSON')?['body']?['count']}<br>\n-----------------------------------------------------------------------------<br>\n@{body('Create_HTML_table_2')}</p>",
                           "Subject": "Sample Test",
                           "To": "<YOUR REQUIRED EMAIL ID>"
                       },
                       "host": {
                           "connection": {
                               "name": "@parameters('$connections')['office365']['connectionId']"
                           }
                       },
                       "method": "post",
                       "path": "/v2/Mail"
                   },
                   "runAfter": {
                       "Create_HTML_table_2": [
                           "Succeeded"
                       ]
                   },
                   "type": "ApiConnection"
               }
           },
           "contentVersion": "1.0.0.0",
           "outputs": {},
           "parameters": {
               "$connections": {
                   "defaultValue": {},
                   "type": "Object"
               }
           },
           "triggers": {
               "manual": {
                   "inputs": {},
                   "kind": "Http",
                   "type": "Request"
               }
           }
       },
       "parameters": {
           "$connections": {
               "value": {
                   "office365": {
                       "connectionId": "/subscriptions/<YOUR SUBSCRIPTION ID>/resourceGroups/<YOUR RESOURCE GROUP>/providers/Microsoft.Web/connections/office365",
                       "connectionName": "office365",
                       "id": "/subscriptions/<YOUR SUBSCRIPTION ID>/providers/Microsoft.Web/locations/northcentralus/managedApis/office365"
                   }
               }
           }
       }
    }