Using the XPATH Web API from Apigize to execute an XPath expression on a JSON message

I really like Web APIs and API Management in Azure and that’s why I created last year the website apigize.com. It is build on Azure and has Web APIs that developers can use to be more productive so that you don't have to develop complicated functionality yourself. I started with a Files Web API and an XSLT Web API that you can use to transform XML messages but of course that is not enough and now it also has an XPATH Web API that you can use to execute an XPath expression on a JSON or XML message!

Normally you can only use XPath to select nodes or node-sets in an XML document but nowadays people use JSON more and more and therefore I also created support for JSON documents. So how does that work?

To explain how you can use XPath in a JSON document I’m going to use the Order XML from the TechNet Wiki Using XPath expressions in BizTalk and transform it to JSON. I used the XPath Web API to do it but you can also use other tools or websites of course. Note that the converted JSON document also must have a root element just like the original XML document.

 

Converting an XML message to JSON with the XPath Web API

It's really simple to convert an XML message to JSON with the XPath Web API. The only thing what you have to do is to set the following header values and post the XML message to the XPath Web API.

Request headers    
Ocp-Apim-Subscription-Key String Subscription key which provides access to this API. Found in your Integration Account.
Content-Type application/xml Specify that the message has XML content.
Accept application/json Specify that the response message must be JSON.
XPath-Expression /Order XPath expression to select the root element in the XML document.

XPATH Web API - Header - XML Request body

 

 

 

 

 

 

Request URL https://free.apigize.io/v1/xpath  
XPATH Web API - Body - Select root node

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Retrieving JSON elements with the XPath Web API

Sending a JSON document to the XPath Web API is almost the same as sending an XML message. You only have to change the Content-Type header. In this example I’m going to use XPath to get all the delivery nodes from the order document which have a deliverytype of Work. (See the TechNet Wiki again for examples of other XPath expressions)

Request headers    
Ocp-Apim-Subscription-Key String Subscription key which provides access to this API. Found in your Integration Account.
Content-Type application/json Specify that the message has JSON content.
Accept application/json Specify that the response message must be JSON.
XPath-Expression /Order/Delivery[DeliveryType='Work'] XPath expression to select the specific delivery node.

XPATH Web API - Header - JSON Request body

 

 

 

 

 

Request body { "Order": { ... } } The JSON message that is going to be sent to the Web API.
XPATH Web API - JSON Body

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Wrap Up

Normally you can only use XPath expressions on XML documents but with the XPath Web API you now are also able to do it on JSON documents. This makes it really easy to select select a specific node or node-sets from a JSON document because you don’t have to write code anymore.

Try it out and register on Apigize because it's free!

About the author

Tomasso Groenendijk lives in Netherlands and is a senior integration consultant at Motion10. He has over 15 years’ experience in software development and software design. Tomasso is specialized in application integration with a strong interest in the Windows Azure cloud platform. In 2014, 2015 and 2016 Tomasso was awarded with the Microsoft Azure MVP award. He is an active contributor to the integration community through his work on his blog, MSDN samples, TechNet Wiki and also speaking on events.

 
 
 
MVP profile Tomasso Groenendijk
 
 
Sentinet Product Specialist
 
 
BizTalk360 Product Specialist

Month List