Azure Logic App - Parse JSON with dynamic key/name

Viewed 170

just want to know if and how I can parse a HTTP response with a dynamic name in a JSON? I used the Azure Management API to receive the managed identities (system- and user assigned managed identities) to receive all managed identities. With a foreach I am iterating the results.

If a resource has a system assigned managed identity and user assigned managed identity, the response looks like this:

{
  "principalId": "<principalId1>",
  "tenantId": "<tenantId>",
  "type": "SystemAssigned, UserAssigned",
  "userAssignedIdentities": {
    "/subscriptions/<subscriptionId>/resourcegroups/<resourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<userAssignedIdentitiesName>": {
      "principalId": "<principalId2>",
      "clientId": "<clientId>"
    }
  }
}

Now, I would like to get the <principalId2>. Unfortunately, the Name of the object is dynamic related to the scope of the resource /subscriptions/<subscriptionId>/resourcegroups/<resourceGroupName>/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<userAssignedIdentitiesName>.

enter image description here

How can I parse the JSON to receive the needed <principalId2>? For all other responses I can easily use the Data operations Parse JSON with the payload I inserted from the HTTP response. Is there a way to use a wildcard? Otherwise, could I somehow just select the first object of userAssignedIdentities to receive the needed value?

1 Answers

Ok, this should work for you. This is the flow I tested with ...

Flow

Initialise JSON

Your JSON as a string, how you do that in your solution may differ slightly.

Initialize XPath Result

Defined as an Array and the expression is ...

xpath(xml(json(concat('{ root: ', replace(variables('JSON'), 'PrincipalId', 'principalId'), '}'))), '(//principalId)[2]')

Initialize Result

A bit more work again but defined as a String and the expression is ...

array(xpath(xml(base64ToString(variables('XPath Result')[0]?['$content'])), '//text()'))[0]

The end result should be your value ...

Result

Related