Send and receive JSON formatted message with BizTalk Server

BizTalk does not send messages natively in JSON format. However, in recent years, JSON has become the format increasingly widespread.

In order to better use the messaging services of BizTalk limiting as much as possible customizations, normally I use the custom pipeline components for both send and receive operations.

The receive statement is very simple since it is sufficient build a pipeline component that converts JSON formatted message into xml and set a target namespace.

The send statement is slightly more complex because you need to instruct the JSON converter for xml nodes of multiplicity greater than one.

This allows you to correctly generate the JSON array.

To do that, you need work inside the schema, where in any element with multiplicity greater than one, you must specify the attribute json:array equal to true.

 

We begin by constructing a schema that defines the namespace attribute in JSON.

 

 

<?xmlversion=1.0encoding=utf-16?>

<xs:schemaxmlns:b=http://schemas.microsoft.com/BizTalk/2003

       xmlns:tns=http://james.newtonking.com/projects/json

       attributeFormDefault=unqualifiedelementFormDefault=qualified

       targetNamespace=http://james.newtonking.com/projects/json

       xmlns:xs=http://www.w3.org/2001/XMLSchema>

  <xs:attributename=Arraytype=xs:boolean />

</xs:schema>

 

 

Once built, you must import it.

 

clip_image001

 

Following the XML result.

 

clip_image002

 

Then you can define the attribute as JSON array.

 

clip_image003

 

Defined the schema, you have to implement the send pipeline component. Below is a code example.

 

 

public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(Microsoft.BizTalk.Component.Interop.IPipelineContext pContext, Microsoft.BizTalk.Message.Interop.IBaseMessage pInMsg)

{

 

       if (pInMsg.BodyPart != null)

       {

 

             XmlDocument xmlMessage = new XmlDocument();

             xmlMessage.Load(pInMsg.BodyPart.GetOriginalDataStream());

 

             foreach (XmlAttribute attribute in xmlMessage.DocumentElement.Attributes)

             {

                    if (attribute.Name != “xmlns:json”)

                    {

                           xmlMessage.DocumentElement.RemoveAttribute(attribute.Name);

                    }

             }

 

             string jsonMessage = JsonConvert.SerializeXmlNode(xmlMessage, Newtonsoft.Json.Formatting.None, true);

 

             pInMsg.BodyPart.Data = new MemoryStream(new UTF8Encoding().GetBytes(jsonMessage));

 

       }

 

       return pInMsg;

}

 

Finally, it is necessary to remove the XML envelope from the WCF layer.

 

clip_image004

 

3 thoughts on “Send and receive JSON formatted message with BizTalk Server

  1. Reblogged this on BizTalk On My Mind and commented:
    JSON has become such common practice that it should really be it’s own adapter in BizTalk! Great article on how you can send JSON requests using BizTalk!

Leave a reply to adityaraina81 Cancel reply