Liquid templates are insufficient
The first reflex was to go for Liquid templates, because the expected output format was JSON. Pretty soon, we realized that Liquid has too many limitations for our scenario:
- Input needs to be transformed into JSON first
- Unreadible syntax if your input XML has complex namespace structures
- Conditional select (e.g. Party with qualifier = BY) not possible without a for each
- No way to inject custom .NET code into Liquid templates
Let’s go with XSLT
As a second attempt, we decided to go for XSLT! We leveraged the Transform XML action, which executes an XSLT mapping. We transformed the EDI XML into the XML representation of the JSON Order, so we could use the built-in json() function to convert the output XML into its JSON format.
A simplified version of the XSLT can be found here. Remark that the output XML does not contain any namespaces and is already in the camelCasing that we expect for the resulting JSON Order:
Issue with XML declaration
Even though we’ve set the omit-xml-declaration attribute to yes, the resulting XML still contained the XML declaration. Apparently this was a bug that has existed for a long time. Fortunately, the product team has solved this bug. In order to remain backwards compatible, they’ve added the “Apply XSLT output attributes” setting to activate the bugfix:
Issue with arrays
The above XSLT mapping, combined with the json() function, worked fine until we faced a transformation of an order that contains a single order line. When there’s only one child record, the json() function has no way to know that we’re dealing with an array. This resulted in invalid JSON Orders that have the orderLines property not defined as an array[].
The observed behavior and a corresponding solution is described over here, in the Newtonsoft knowledge base. Assuming that Logic Apps uses Newtonsoft behind the scenes, we tried the solution:
- Declare the following namespace prefix:
xmlns:json=”http://james.newtonking.com/projects/json” - Add the following Array attribute to the orderLines XML record:
<orderLines json:Array=’true’>
And guess what!? It worked as a charm!
Conclusion
As you could see, there were quite some pitfalls when executing complex transformations in Logic Apps. I hope this blog post will save you some headaches! Special thanks to my helpful colleagues Maxim, Iain and Pim for their assistance on this matter.
Cheers,
Toon
Subscribe to our RSS feed