Consuming JSON endpoint with BizTalk 2010 R2 REST WCF-WebHttp adapter

In the previous article we looked at some of the basic GET operations we can perform using the new REST (WCF-WebHttp) adapter that ships with BizTalk Server 2010 R2.  All our examples are based on XML data formats.

One of the readers asked the question in the comments, how we can consume a REST end point that returns the data in JSON format. Let’s take a look at how easy it’s to consume JSON end points

What is JSON?

There is a complete site dedicated to JSON http://www.json.org/. In brief, JSON is a light weight data interchange format, that’s easy for humans to read and write, and it’s also easy for systems to generate and parse. It’s mainly used in Web world with client side java script and it’s also widely used in mobile devices communication. The main benefit of JSON format is the reduced data size, with XML there are lots of  repeating unnecessary element and attributes names. With mobile devices usage of bandwidth is critical and it’s important to reduce the data set size, hence JSON is popular. It’s also popular in areas where you are dealing with large set of data. Example: If you are trying to plot a chart at client place with the data coming from server, it makes sense to use JSON format, avoiding lot of unnecessary boiler plat element/attribute names.

Simple GET JSON Response

Most of the REST endpoints these days will support returning the data in JSON and XML format. For example in our previous article we used twitter API’s as example. Twitter supports both XML and JSON formats. As shown below the first screen shot shows the XML format returned and the second screen shot shows the JSON format returned, just by making a slight modification in the URL.

http://api.twitter.com/1/statuses/user_timeline.xml?screen_name=biztalk360

image

http://api.twitter.com/1/statuses/user_timeline.json?screen_name=biztalk360 (note the change from .xml to .json)

image

Now let’s take a look at how we can handle the JSON formatted message within BizTalk. We are going to discuss few options here, you need to choose the data format appropriately based on the consumer of the message and how are going to deal with it. Don’t do unnecessary transformations, example if the end point supports XML formatting and you are interested in XML formatting simply use XML. Do not try to consume JSON message and do the conversions to XML.

Scenario 1: Don’t touch the JSON message

There will be scenarios where BizTalk just acts as a middleware platform and work just as a conductor hooking things up. In this case there is no necessity to manipulate anything in the message in BizTalk, you simply receive the response from the end point and hand it back to the consumer as shown in the below picture.

image

If this is the case you don’t need to do anything special, you configure your WCF-WebHttp adapter (for GET) exactly the same way we explained in the article REST (WCF-WebHttp) adapter in BizTalk Server 2010 R2 (but using JSON end points) and things should work. This is mainly because we used PassThru pipeline on the two-way WCF-WebHttp send adapter and it simply published the JSON message back into the message box.

Scenario 2: Convert JSON to XML

There may be scenarios where you wanted do something with the JSON message. The REST end point may be providing only JSON data format and you are in a situation to manipulate certain things within BizTalk server. BizTalk as such can’t understand JSON message and it needs to be converted to known format like XML, Flat-File etc.  There are few options you can use to convert the JSON message to XML

  • Writing a custom end point behaviour
  • Writing a custom pipeline component
  • Converting inside Orchestration

Writing a custom end point behaviour

There will always be scenarios where certain level of manipulation is required to the request and response messages we send/receive from the end point. BizTalk REST WCF-WebHttp adapter (or any WCF adapter for that matter) provides an extensibility mechanism using the EndPointBehaviour  extension. You can implement your serialization logic inside the AfterReceiveReply override and configure it in the adapter. We are not going to discuss in detail about this option here.

public void AfterReceiveReply(ref Message reply, object correlationState)

{

// do your json to xml serialization

}

image

Writing a custom pipeline component

The second option is to use a custom pipeline component which does the conversion from the returned JSON format to XML. There is an excellent open source library called JSON.NET in codeplex, which greatly reduces our task. You can create custom pipeline component with few lines of code as shown below, to convert from JSON to XML

image

and you can configure the pipeline in your WCF-WebHttp port as shown below

image

Once configured the response message coming out of the two-way send port will be in an XML format as shown below, which you can easily manipulate within your BizTalk solution.

image

Converting inside Orchestration

If you don’t want to go through either EndpointBehviour or Custom pipeline component/pipeline route, then the easy option is consume the message inside your orchestration and utilize a helper .NET object to do the same serialization what we have done inside the pipeline component.

About Saravana Kumar

Saravana Kumar is the Founder and CTO of BizTalk360, an enterprise software that acts as an all-in-one solution for better administration, operation, support and monitoring of Microsoft BizTalk Server environments.

Comments

  1. Dan Rosanova says:

    Awesome article Saravana! Great work.

  2. preps2 says:

    Great Work !

  3. Guru says:

    Great Article Saravana. Keep up the good work.
    -Guru

  4. Himanshu Thawait says:

    Now days Restful service specially JSON base getting popular and I was think about this kind of scenario from BizTalk point of view and got this Article, Thanks Sravana!!

  5. Chirag Shah says:

    hi i am trying to send JSON message to a service thru WCF-Custom binding but i am getting error.Does WCF-Custom understyand JSON and will it work

  6. Praveen says:

    Would it be possible to get a copy of this project?
    Thanks,
    Praveen

  7. Mikael says:

    Thanks for the example.

    /Mikael

  8. Jerome says:

    Hi, thanks for your great article.
    I’m trying to do something similar with BizTalk 2010.
    I want to convert my JSON message inside my orchestration, but it seems that BizTalk automatically convert the JSON message to Xml (in a WCF-Custom port with wsHttpBinding)

    Do you know any options to prevent BizTalk to convert JSON to Xml?

Trackbacks

  1. […] In this post we will discuss dealing with incoming JSON requests, such that they become readable/map-able XML for which we can generate a schema. Ultimately there are two ways that this can be accomplished. Both of these methods have actually already been extensively documented. The first method is by using a custom pipeline component (demonstrated here by Nick Heppleston), the second is by extending WCF with a custom behavior (briefly discussed here by Saravana Kumar). […]

Speak Your Mind

*