Tutorial: XMLToJSON and JSONToXML policies in Apigee Edge

The more APIs and services you have to deal with, the more likely it is that you will want to connect with a service that delivers data in format you don't want, or accepts data in a format you don't want. The most common cases here are XML and JSON. Imagine a data service that delivers plain-old-XML (sometimes called POX) and you'd prefer that clients can receive JSON. Or vice versa.

Apigee Edge includes policies out of the box that help solve that problem. Insert an Apigee Edge API Proxy between the client and the backend system and you can easily transform the data format in the way you prefer. The policies are: XMLToJSON and JSONToXML.

In Apigee Edge, policies are basic building-blocks that perform one bit of function. These data format conversion policies can be included into any API Proxy to help you build the API of your dreams! For example, these policies might be used to build a RESTful API Proxy that fronts an old SOAP system. With that kind of facade, new clients can send and receive JSON, even though the backend is SOAP! Without changing the backend!

OK, let's see how they work. The good news is they're really, really simple. Here's the example configuration for XMLToJSON:

<XMLToJSON name="XMLToJSON-1">
  <Source>response</Source>
  <OutputVariable>response</OutputVariable>
</XMLToJSON>

Pretty simple, huh? There are some key points here. The SOURCE for the transformation policy is a message. In this case it is "response". This refers to the response message, which means this policy will work only if you anchor it into the response flow. You can also choose "request" here, or "message". In all cases, the source and Output must be message variables.

Why is this relevant? Let's suppose you wish to perform XML-to-JSON conversion for a ServiceCallout. In that case you will need to retrieve the response from the ServiceCallout into a named context variable, then use that context variable in the XMLToJSON configuration. For example, suppose this is the ServiceCallout config:

<ServiceCallout name='SC-1'>
  <Request variable='authenticationRequest'>
        <Set>
          <Payload contentType='application/xml'>
            <request>
              <grant_type>client_credentials</grant_type>
              <client_id>whatever</client_id>
              <client_secret>{variable-that-holds-secret}</client_secret>
            </request>
          </Payload>
         <Verb>POST</Verb>
         <Path>/endpoint/gov1/token</Path>
      </Set>
  </Request>
  <Response>tokenResponse</Response>
  <HTTPTargetConnection>
    <Properties>
      <Property name='success.codes'>2xx, 4xx, 5xx</Property>
    </Properties>
    <URL>https://api.example.com/</URL>
  </HTTPTargetConnection>
</ServiceCallout>  

You can see that the response is being placed into a new context variable named 'tokenResponse'. In that case, supposing the response is in XML format, we can follow that policy with an XMLToJSON like this:

<XMLToJSON name="XMLToJSON-1">
  <Source>tokenResponse</Source>
  <OutputVariable>tokenResponse</OutputVariable>
</XMLToJSON>

The outputvariable must exist! And it can be the same as the input variable.

The JSONToXML works the same way. You might use this to convert inbound JSON request bodies that will be sent to backends that expect XML. The configuration is very similar to the XMLToJSON policy. JSONToXML looks like this:

<JSONToXML name='JSONToXML-1'>
  <Source>request</Source>
  <OutputVariable>request</OutputVariable>
</JSONToXML>  

Here is a screencast showing you how to use these policies in working API Proxies:

click here to open that in a new window.

OK, so that covers the basics of the XMLToJSON policy and the JSONToXML policy in Apigee Edge.

Some related links:

  1. Using JSONToXML and XMLToJSON to manually build a facade for a SOAP backend.
  2. For information on the TreatAsArray option, useful when transforming XML data into JSON arrays, see this article.
  3. For hints on transforming JSON to JSON, see this article + screencast.
Version history
Last update:
‎01-09-2017 11:59 AM
Updated by: