When we talked about triggering Power Automate on Associate/Disassociate, I did set aside the task of “dealing with horrendous serialization”. Short version: it’s a RemoteExecutionObject that is serialized in the message and every DataCollection property is serialized as an array of objects with the key and value properties.
"InputParameters": [
{
"key": "Target",
"value": {
...
"LogicalName": "adx_webrole"
}
},
{
"key": "Relationship",
"value": {
...
"SchemaName": "adx_webrole_contact"
}
}
Fair enough. But in the jsonified world of Power Automate, it would’ve been nice to see it as { “key”:”value” } object instead:
"InputParameters": {
"Target": {
...
"LogicalName": "adx_webrole"
},
"Relationship": {
...
"SchemaName": "adx_webrole_contact"
}
}
Let’s say we want to extract SchemaName of the relationship. What are our options?
Someone suggested iterating over the InputParameters array until Relationship key is found and then extract the value and the property. It will work and will be very readable but bleugh! Here is not one but two methods to achieve the same.
Method 1: Filter array
I have to admit that I did not known about the Filter Array action because I never had the need to use it.
- Get the InputParameter array
- Filter array and only select the items where
item()?[‘key’] is equal to Relationship - In theory we should have only one item in the resulting array so we go straight for the first item and it’s
first( body(‘MatchingItems’) )?[‘value’]?[‘SchemaName’]
Method 2: To XML and back
If you’ve been around long enough you’d know that the roots of the context object are in XML. And we have a rarely used xpath function (all cool kids have been named json). Can we build a single expression that does the job? Why not!
Let’s digest it:
- First I want an empty object hence json(‘{}’).
- I add root property to that object with the value of the serialized RemoteExecutionObject. We need that property because xml function wants a single root element.
- xml converts that object into, well, xml.
- xpath(xmlstuff, ‘//SchemaName/text()’) says “give me the text content of the SchemaName elements anywhere in the hierarchy. I know there is only one of this so we’re safe to use that.
- That returns, again, an array and we grab the first element.
I feel that the first 3 steps is an overkill and a side effect of a brain fart. Let me know if there is a simple way to convert triggerBody into (kind of) {“root”: triggerBody()}.
Does it work? You betcha:
Cover photo Staff Sgt. Jacob N. Bailey / Public domain