2

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?

2
  • Is it completely dynamic or do you know the values it will potentially be? I’m asking but I’m 95% sure it’s the former and just want to be sure. Commented Apr 26, 2022 at 7:07
  • Hey, it is somehow dynamic. This was the reason why I asked if wildcards in Parse Json are supported. Is looks like this /subscriptions/<subscriptionId>/resourcegroups/<ResourceGroupName/providers/Microsoft.ManagedIdentity/userAssignedIdentities/<userAssignedIdentitiesName>. Only the subscriptionId, ResourceGroupName and userAssignedIdentitiesName is dynamic. Commented Apr 26, 2022 at 7:12

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

Just another question cause the ARM API is responding strange.. It seems that it is case sensitive to identify the name. Sometimes the API is responding, for whatever reason, with PrincipalId instead of principalId. Then it won't find the value. I mean I can use as a workaround a if-condition, but you might have also an answer on this?
I've been dicking around with XPath to see if I can get it to work but it doesn't like anything I give it. Therefore, the easiest thing to do would be to set any PrincipleId text to lower case and go from there. I'll update my answer.
Check the updated answer for the middle Initialize XPath Result step in the flow. Tested and it worked for me, you may need to test further with real data but I suspect it will be ok. I also updated the last step, I overcooked the solution.
I just saw, that when more than one userAssignedIdentities are in the results the foreach for xPath Result will automatically decode the base64 to string. In this case, you need to use array(xpath(xml(outputs('Compose_7')), '//text()'))[0] to get the text within the xml-attribute <principalId></principalId>
Oh ok, nice. Yeah, I could only work on what you gave me so you've managed to take it a step further with real data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.