Was this helpful?

Apigee Edge enables developers to convert messages from the extensible markup language (XML) format to JavaScript object notation (JSON) format using the XMLToJSON policy type. This policy is useful for enabling backend XML services to support RESTful apps that require JSON (for example, due to a lack of an XML parsing capabilities on the client).

In a typical mediation scenario, a JSONToXML policy on the inbound request flow is often paired with an XMLToJSON policy on the outbound response flow. By combining policies this way, a JSON API can be exposed for backend services that natively support only XML.

For scenarios where APIs are consumed by diverse clients apps which may require either JSON and XML, the format of the response can be dynamically set by configuring JSONToXML and XMLToJSON policies to execute conditionally. See Flow variables and conditions for an implementation of this scenario.

The HTTP Content-type header of the source message must be set to text/xml. If the HTTP Content-type header of the source message is not text/xml, the policy is not enforced.

The payload of the XML message is parsed and converted into JSON, and the HTTP Content-type header of the XML-formatted message is set to application/json.

Sample

This policy type defines a set of reasonable defaults. The minimal policy configuration required to convert XML to JSON is the following:

<XMLToJSON name="ConvertToJSON">
  <Options>
  </Options>
  <OutputVariable>response</OutputVariable>
  <Source>response</Source>
</XMLToJSON>

Assuming that the intent is to convert an XML-formatted response into a JSON-formatted response, the policy would be attached to a response Flow (for example, the ProxyEndpoint response PostFlow). This configuration takes a JSON-formatted response message as the source, and then creates an XML-formatted message that is populated in the response OutputVariable. Apigee Edge automatically uses the content of this variable as the message for next processing step.

Configuring an XML to JSON policy

For more advanced control over mapping XML to JSON structures, use the following configuration elements. All fields are optional. However, if you specify NamespaceBlockName, you must also specify DefaultNamespaceNodeName and NamespaceSeparator.

Field Name Description
Source
The variable, request or response, that contains the XML message that you want to convert to JSON.
  • If source is not defined, then it is treated as message (which resolves to request when the policy is attached to a request flow, or response when the policy is attached to a response flow). Usually, you set this to be request or response, depending on whether you need to convert an inbound XML request, or an outbound XML response, into JSON.
  • If the source variable cannot be resolved, or resolves to a non-message type, the policy will throw an error.
OutputVariable
Stores the output of the XML to JSON format conversion. This is usually the same value as the source, that is, usually inbound XML request in converted to an inbound JSON request,
If OutputVariable is not specified, the source is treated as OutputVariable. For example, if the source is response, then OutputVariable defaults to response.
Options
The following configuration elements can be added as children of the Options element. All options are optional, however, at a minimum, an empty Options element must be present for a policy to be valid.
RecognizeNumber
Valid values: true false
Default is false.
If true, then number fields in the XML payload retain their original format.
Consider the following XML example:
<a>
  <b>100</b>
  <c>value</c>
</a>
If true, converts to:
{
    "a": {
        "b": 100,
        "c": "value"
    }
}
If false, converts to:
{
    "a": {
        "b": "100",
        "c": "value"
    }
}
RecognizeBoolean
Valid values: true and false
The default value is

false

For the following XML example:
<a>
  <b>true</b>
  <c>value</c>
</a>
If true, converts to:
{
    "a": {
        "b": true,
        "c": "value"
    }
}
If false, converts to:
{
    "a": {
        "b": "true",
        "c": "value"
    }
}
RecognizeNull
Valid values: true and

false

Default value is

false

.
For the following XML:
<a>
  <b></b>
  <c>value</c>
</a>
If true, converts to:
{
  "a": {
    "b": null,
    "c": "value"
  }
}
If false, converts to:
{
  "a": {
    "b": {},
    "c": "value"
  }
}
NullValue Indicates a null value. By default the value is NULL.
NamespaceSeparator
Consider the following XML example:
<a xmlns="http://ns.com" xmlns:ns1="http://ns1.com">
  <ns1:b>value</ns1:b>
</a>
If NamespaceSeparator is not specified, the following JSON structure is generated:
{
    "a": {
        "b": "value"
    }
}
If the elements NamespaceSeparator, NamespaceBlockName, and DefaultNamespaceNodeName are specified as %, #namespaces, and &, respectively, then the following JSON structure is generated:
{
    "a": {
        "#namespaces": {
            "&": "http://ns.com",
            "ns1": "http://ns1.com"
        },
        "ns1%b": "value"
    }
}
NamespaceBlockName
DefaultNamespaceNodeName
TextAlwaysAsProperty
Valid values: true/false
Default is false.
If set to true, the content of the XML element is converted to a string property.
For the following XML:
<a>
  <b>value1</b>
  <c>value2<d>value3</d>value4</c>
</a>
If TextAlwaysAsProperty is set to true and TextNodeName specified as TEXT, the following JSON structure is generated:
{
  "a": {
    "b": {
      "TEXT": "value1"
    },
    "c": {
      "TEXT": [
        "value2",
        "value4"
        ],
        "d": {
          "TEXT": "value3"
        }
      }
    }
}
If TextAlwaysAsProperty is set to false and TextNodeName specified as TEXT, the following JSON structure is generated:
{
  "a": {
    "b": "value1",
    "c": {
      "TEXT": [
        "value2",
        "value4"
      ],
      {
        "d": "value3",
      }
    }
}
TextNodeName
AttributeBlockName
Consider the following XML example:
<a att1="value1" att2="value2"/>
If both the attributes (AttributeBlockName and AttributePrefix) are specified as defined in the XML to JSON example, the following JSON structure is generated:
{
  "a": {
    "ATT_BLOCK": {
      "@att1": "value1",
      "@att2": "value2"
    }
  }
}
If only AttributeBlockName is specified, the following JSON structure is generated:
{
    "a": {
        "ATT_BLOCK": {
            "att1": "value1",
            "att2": "value2"
        }
    }
}
If only AttributePrefix is specified, the following JSON structure is generated:
{
    "a": {
        "@att1": "value1",
        "@att2": "value2"
    }
}
If neither is specified, the following JSON structure is generated:
{
    "a": {
        "att1": "value1",
        "att2": "value2"
    }
}
AttributePrefix
OutputPrefix
Consider the following XML example:
<a>value</a>
If both the attributes (OutputPrefix and OutputSuffix) are specified as defined in the XML to JSON example, the following JSON structure is generated:
PREFIX_{
    "a": "value"
}_SUFFIX
If only OutputPrefix is specified, the following JSON structure is generated:
PREFIX_{
  "a" : "value"
}
If only OutputSuffix is specified, the following JSON structure is generated:
{
  "a" : "value"
}_SUFFIX
If neither OutputPrefix nor OutputSuffix is specified, the following JSON structure is generated:
{
    "a": "value"
}
OutputSuffix

Policy-specific error codes

The default format for error codes returned by Policies is:

{
  "code" : " {ErrorCode} ",
  "message" : " {Error message} ",
  "contexts" : [ ]
}

The XMLToJSON Policy type defines the following error codes:

Error Code Message
FormatUnavailable XMLToJSON[{0}]: Format {1} is not available
SourceUnavailable XMLToJSON[{0}]: Source {1} is not available
ExecutionFailed XMLToJSON[{0}]: Execution failed. reason: {1}
InvalidSourceType JSONToXML[{0}]: Invalid source type {0}. Valid source types are {1}.
InCompatibleTypes JSONToXML[{0}]: String can not be assigned to message type.
OutputVariableIsNotAvailable JSONToXML[{0}]: Output variable is not available.

Policy schema

Each policy type is defined by an XML schema (.xsd). For reference, policy schemas are available on GitHub.

Comments

Please convert this XML code in to JSON code through C# or ASP.net

10/27/2013
10/31/2013
10/27/2013
10/31/2013
10/27/2013
10/31/2013
UK
London (All)
DUBAI
Dubai

2
0
0

0
0
0

0
0
0

Any
ANY
Any
ANY
FALSE
AwayHoliday-F+H
FULL
YES
YES

Hi Piyush - Please post this question to Apigee Support at http://community.apigee.com/content/apigee-customer-support. Thanks.

Add new comment

Provide your email address if you wish to be contacted offline about your comment.
We will not display your email address as part of your comment.

We'd love your feedback and perspective! Please be as specific as possible.
Type the characters you see in this picture. (verify using audio)

Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.