Parsing JSON Array Messages

Posted: July 21, 2018  |  Categories: Azure BizTalk

This post shows how to parse a message that starts and end as an array with BizTalk Server. Furthermore I show how a Azure Logic App solution achieves this with much less effort.

#1 Azure Monitoring Platform

The JSON Array Message

My last blog post describes a web service that returns a JSON array of orders. An empty array returns if no orders are found. Click on the picture below to download the order file to look at it more closely

Processing the JSON using BizTalk Server

Attempting to convert this message into XML using a pipeline containing the standard JSON decoder fails spectacularly. The error in the event log is “XmlNodeConverter can only convert JSON that begins with an object.“.  This leads us to a solution. The solution is to wrap the array with an object before trying to process it. Replacing the starting square bracket with { “orderMessage”: [  and the ending square bracket with ]} the message now parses into XML without failure.

Two ways to do this are using a WCF Behaviour  or a custom pipeline component . A recent post by Boutaleb Hicham shows a nice example of a custom WCF behaviour that handles a null returned from an API. Refactoring this to our situation should be easy. Instead, I am going to show the second option using the BRE Pipeline Component. Please view Ahmed Taha’s Integration Monday presentation here if you are not familiar with this pipeline component.

Additionally, any solution must handle an empty array.

My BizTalk Server Solution

The solution starts with a WCF-SQL polling adapter and my previous post describes this. In the next step, a WCF-WebHTTP adapter sends the request to the restaurant API to get the orders.  A file adapter  writes the orders as one file to the file system. The receive pipeline contains a BRE pipeline component.

Enabling this to true and configuring an action to run a regex (\A\[\]\Z) over the incoming JSON to remove [] sets us up to handle the case when no orders are found. This creates a file of zero bytes if there is no orders. A file containing orders passes through the pipeline unchanged.

Subsequently a send file adapter writes the JSON orders file to the file system. The pipeline is essentially passthrough but also removes  any byte order mark. Now all the JSON files in the folder contain several orders or a zero byte files.

A receive file adapter consumes the files and magically all the zero byte files disappear.  A file adapter deletes a zero byte file. All the other files that start and end as an array process through custom pipeline converting the orders JSON to XML. An XML dissembler  de-batches them into individual orders for further processing. The custom pipeline contains a BRE pipeline component, a JSON decoder and an XML dissembler.

A business rule fires on this pipeline with an regex action. This replaces the starting bracket with {“orderMessage” [  and the ending bracket with }].

Secondly a JSON decoder converts the JSON to XML. This is an envelope containing many orders. Creating the correct envelope schema, order schema and body XPath allows de-batching into individual orders. Finally the XML orders arrive in the BizTalk message box for further processing.

In summary the solution above shows how BizTalk server can convert a JSON array  into XML for further processing.

Logic App solution

The Logic App solution was a snip to set up and within 30 minutes I had it up an running. Chaining two Logic Apps  together places individual orders on service bus queue. We saw the first logic App before in my previous blog on HTTP polling. The second Logic App does the JSON parsing and de-batching.  Running all this in one Logic App is possible but I like using a service bus as a point of persistence.

The second logic app triggers when a JSONOrder is arrives on the orders service bus queue. The next step parses the service bus messages  as JSON.  Converting the base64 service bus message to JSON using json(base64ToString(triggerBody()?[‘contentData’])) must be done first. Thirdly a for each action de-batches JSON orders into many JSON order messages. Finally the JSON order awaits further processing after saving to another service bus queue.

It is gratifying that submitting a blank array, the result of finding no orders does not cause an error or add any message to the service bus queue.

As an aside changing the response to null when the no orders are found causes an error. I will talk to the third party API provide about reverting back to sending a blank array.

In summary, the solution above shows how Azure Logic Apps can convert a JSON array  into XML for further processing with no code and just a few simple actions.

#1 Azure Monitoring Platform


Once again the Logic app solution wins hands down with respect to brevity, simplicity and time to deliver. Why would anyone bother doing this in BizTalk server now?


Back to Top