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 … Continue reading 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.
(note the change from .xml to .json)
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.
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.
// do your json to xml serialization
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
and you can configure the pipeline in your WCF-WebHttp port as shown below
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.
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.