Shaping, accessing, and converting messages

You're viewing Apigee Edge documentation.
Go to the Apigee X documentation.
info

You can use policies included with Apigee Edge 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 Edge 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 Edge includes policies that make it easier to convert between XML and JSON, and to transform XML with XSL.

Converting JSON to XML

The JSON to XML 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>
     <OutputVariable>request</OutputVariable>
</JSONToXML>

Converting XML to JSON

The XML to JSON 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">
   <OutputVariable>response</OutputVariable>
   <Source>response</Source>
</XMLToJSON>

Transforming XML with XSL

You can use the XSL Transform 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">
    <ResourceURL>xsl://my_transform.xsl</ResourceURL>
    <Source>response</Source>
    <OutputVariable>response</OutputVariable>
</XSL>

    <Source>request</Source>
    <ResourceURL>xsl://XSL-Transform-1.xsl</ResourceURL>
    <OutputVariable/>

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.