Shaping, accessing, and converting messages

This page applies to Apigee and Apigee hybrid.

View Apigee Edge documentation.

You can use policies included with Apigee to manipulate the messages flowing through your API proxies. With policies, you can:

  • Convert messages between formats, such as from XML to JSON.
  • Set variable values from message content, and create messages from variable values.
  • Use procedural code, such as JavaScript, Java, and Python, to handle messages and data in more complex ways.

In general, when using these policies, you specify input and output as a flow variables. At run time, Apigee retrieves the input value from a source variable and writes the output value to an output variable.

This topic introduces these capabilities. For more complete and technical information, see the policy references for each.

Simple handling for XML and JSON

Apigee includes policies that make it easier to convert between XML and JSON, and to transform XML with XSL.

Converting JSON to XML

The JSONtoXML policy takes your JSON input and converts it to XML.

In the following policy example, the policy takes a JSON-formatted message from the request variable, then replaces the variable value with an XML-formatted message. The input is specified in the <Source> element, while the output is specified in the <OutputVariable> element.

<JSONToXML name="jsontoxml">
     <Source>request</Source>                     ---> INPUT
     <OutputVariable>request</OutputVariable>          OUTPUT --->
</JSONToXML>

Converting XML to JSON

The XMLtoJSON policy converts XML input message into JSON.

In the following policy example, the policy takes an XML-formatted message from the response variable, then replaces the variable value with a JSON-formatted message. The input is specified in the <Source> element, while the output is specified in the <OutputVariable> element.

<XMLToJSON name="ConvertToJSON">
   <Source>response</Source>                     ---> INPUT
   <OutputVariable>response</OutputVariable>          OUTPUT --->
</XMLToJSON>

Transforming XML with XSL

You can use the XSLTransform policy to transform messages using XSL. For example, after converting a JSON payload to XML, you can use the XSL Transform policy with a style sheet to perform the transformation you need.

In the following policy example, the policy takes an XML-formatted message from the response variable, then replaces the variable value with a message formatted by my_transform.xsl. The input is specified in the <Source> element, while the output is specified in the <OutputVariable> element.

<XSL name="TransformXML">
  <Source>response</Source>                        ---> INPUT
  <ResourceURL>xsl://my_transform.xsl</ResourceURL>       --TRANSFORMER--
  <OutputVariable>response</OutputVariable>                            OUTPUT --->
</XSL>

Handling variable data

Data handling within a proxy often involves simply working with state data as flow variable values. You can often do this by using a policy that gets or sets variable values. For example, you might want to shape variable values into a message, or extract the contents of a message to set variable values.

Be sure to see references for the following two policies:

Creating more complex transformations

For data handling whose complexity is outside the capability of included policies, you can use procedural languages such as JavaScript, Java, or Python.

Generally, this involves writing code in one of these languages, then invoking the code from a policy specific to the language. For more about this, see Incorporating procedural code.