Handle Array/List Json messages in BizTalk Receive Pipeline

This blog is to explain how to handle below requirements in BizTalk Receive Pipeline.

  1. Handle Non-List/Array json messages
  2. Handle List/Array json messages
  3. Remove special characters before Json Decoder pipeline stage

Let’s discuss these points.

  1. Handle Non-List/Array json messages

By default, BizTalk Receive Pipeline Json Decoder can convert the Json message to Xml message.

Example:

{

    “ForeignTax”: 12.5,

    “AdditionalPremium”: 20

}

2. Handle List/Array json messages

By default, BizTalk receive pipeline Json Decoder cannot convert Array/List Json messages to Xml format.

Example:

[

    {

        “Id”: 94,

        “LineOfBuinsinesses”: [

            {

                “Id”: 1,

                “Description”: “abc”,

                “Type”: 1

            },

            {

                “Id”: 2,

                “Description”: “def”,

                “Type”: 2

            },

            {

                “Id”: 3,

                “Description”: “ghi”,

                “Type”: 2

            }

        ]

    }

]

In order to handle this kind of json messages, we need to add one Root element to the message and this can be done using custom pipeline. You can download the custom pipeline code from below link.

DownLoad

Below is the main code for this custom pipeline:

        public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)

        {

            pInMsg.BodyPart.Data = EnhanceMessage(pInMsg.BodyPart.Data);

            return pInMsg;

        }

        private Stream EnhanceMessage(Stream stream)

        {

            StreamReader reader = new StreamReader(stream);

            string text = reader.ReadToEnd();

            MemoryStream resultingStream;

            if (text.StartsWith(“[“))

            {

                text = string.Format(“{{{0}:{1}}}”, RootNode, text);

                string invalidCodeRegEx = @”[\\][u][0-9]{4}|\\b”;

                text = System.Text.RegularExpressions.Regex.Replace(text, invalidCodeRegEx, “”);

                byte[] byteArray = Encoding.UTF8.GetBytes(text);

                resultingStream = new MemoryStream(byteArray);

                resultingStream.Position = 0;

            }

            else if(string.IsNullOrEmpty(text)||string.IsNullOrWhiteSpace(text))

            {

                text = “{}”;

                byte[] byteArray = Encoding.UTF8.GetBytes(text);

                resultingStream = new MemoryStream(byteArray);

                resultingStream.Position = 0;

            }

            else

            {

                byte[] byteArray = Encoding.UTF8.GetBytes(text);

                resultingStream = new MemoryStream(byteArray);

                resultingStream.Position = 0;

            }

            return resultingStream;

        }

We need to add this pipeline to Receive pipeline decode stage and deploy the application.

You need to supply the RootName parameters from Pipeline properties in BizTalk admin console.

1.png

In the above screenshot, you need to provide RootNode under Decode Stage.

After executing this receive pipeline Enhance JSON Stage, your json message looks like something below and this message can be handled by JSON Decoder.

{“Details”: [

    {

        “Id”: 94,

        “LineOfBuinsinesses”: [

            {

                “Id”: 1,

                “Description”: “abc”,

                “Type”: 1

            },

            {

                “Id”: 2,

                “Description”: “def”,

                “Type”: 2

            },

            {

                “Id”: 3,

                “Description”: “ghi”,

                “Type”: 2

            }

        ]

    }

]}

Also, you can use the same pipeline for both List and Non-List messages, only thing you have to do for Non-list message is that you no need to supply any Root parameter.

3. Remove special characters before Json Decoder pipeline stage

If you want to remove some special characters in the Json message, you can use some regular expression in the custom Pipeline code.

Example:

string invalidCodeRegEx = @”[\\][u][0-9]{4}|\\b”;

                text = System.Text.RegularExpressions.Regex.Replace(text, invalidCodeRegEx, “”);

One response to “Handle Array/List Json messages in BizTalk Receive Pipeline”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: