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, “”);

5 responses to “Handle Array/List Json messages in BizTalk Receive Pipeline”

  1. […] Click here for cusom pipeline for below rquirement […]

    Like

  2. How the name ‘Enhance JSON’ is showing the screenshot even though the name in the downloadable code is ‘EnhanceforListNode JSON’ ?

    Like

    1. In downloaded code, Name function has defined as EnhanceforListNodeJSON. you can choose name as per your convenience.

      Like

  3. Thank you for your code. Its an excellent piece of work. By any chance do you know how we can update the existing Custom Pipeline component which was in use but now it need few modification. I was able to do that only after removing the custom pipeline from BizTalk resource. Is there any other way ?

    Like

    1. If there is only change in logic, and not change in version and name or configuration, you just need to GAC the dll and restart host instances, BizTalk will take the latest one, no need to remove from biztalk resource

      Like

Leave a reply to Create Schema from JSON files: Cancel reply

Trending