The template language function 'xml' parameter is not valid. The provided value cannot be converted to XML: JSON root object has multiple properties.


Issue


While working on a POC about Debatching in Logic Apps using For Each,  I was encountered with a below error when testing it



The template language function 'xml' parameter is not valid






"InvalidTemplate. Unable to process template language expressions for action 'For_each' at line '0' and column '0': 'The template language function 'xml' parameter is not valid. The provided value cannot be converted to XML: 'JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifying a DeserializeRootElementName. Path 'inputs'.'. Please see https://aka.ms/logicexpressions#xml for usage details.'."


Why it happened


As I had to debatch an xml message coming as payload in trigger, following xpath expression was provided to ForEach action i.e.

xpath(xml(trigger()),'//*[local-name()="PurchaseOrder" and namespace-uri()
="http://www.adventure-works.com"]')


And when xml message was posted, the logic app was not able to apply
xpath on the trigger output.

It happened because trigger() is an Azure Workflow built-in FUNCTION,
which refers the entire trigger object (including headers and body).

Thus at runtime, when expression tried to apply XML() function which
expects string it found JSON trigger Object.

So error was returned -

 The provided value cannot be converted to XML: 'JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document.


What to do


The xml payload which is passed along with trigger goes into trigger().outputs.body at runtime.

And triggerBody() is shorthand for it and it's type is String, so either of it has to be provided  to XML() function .


So the correct expression is


xpath(xml(triggerBody()),'//*[local-name()="PurchaseOrder" and namespace-uri()
="http://www.adventure-works.com"]')

In the above expression first triggerBody() (which is string) is casted in XML using xml() function and then xpath function is applied.


That's it, all worked fine.


Post a Comment

If you have any suggestions or questions or want to share something then please drop a comment

Previous Post Next Post