Policies
XML to JSON Outbound Policy
This policy is useful for converting legacy XML or SOAP APIs into modern REST APIs. It can be useful to add a custom outbound policy that runs after this policy to further transform the raw converted content into something more user friendly.
Configuration
The configuration shows how to configure the policy in the 'policies.json' document.
Policy Options
The options for this policy are specified below. All properties are optional unless specifically marked as required.
removeNSPrefix
<boolean> -Remove namespace string from tag and attribute names.Defaults totrue
.ignoreProcessingInstructions
<boolean> -Ignore processing instruction tags. i.e.Defaults to<?elementnames <fred>, <bert>, <harry> ?>
.true
.ignoreDeclarations
<boolean> -Ignore declarations. i.e.Defaults to<?xml version="1.0"?>
.true
.ignoreAttributes
<boolean> -Ignore tag attributes.Defaults totrue
.stopNodes
<string[]> -At particular point, if you don't want to parse a tag and it's nested tags then you can set their path in stopNodes. You can also set tags which should not be processed irrespective of their path using * as the wildcard.attributeNamePrefix
<string> -The prefix of attribute names in the resulting JS object.Defaults to"@_"
.textNodeName
<string> -Text value of a tag is parsed to #text property by default.Defaults to"#text"
.trimValues
<boolean> -Remove surrounding whitespace from tag or attribute value.Defaults totrue
.parseOnStatusCodes
<string | number[]> -A list of status codes and ranges "200-299, 304" that should the XML parser should run on. If not set, the parser will run on all status codes.
Using the Policy
This policy can help expose legacy XML or SOAP APIs using modern JSON REST APIs. The policy is configurable in ways that make it easier to strip parts of the XML document that you are unlikely to use, such as processing instructions, namespaces, or directives. The default options for this policy will generally give you a fairly clean output. However, it is likely that the output of the raw conversion is still not in the best format.
The best way to clean up the output of your XML is first, run this policy, then add a custom code outbound policy that further reshapes the JSON data structure. An example policy that cleans up a SOAP response is shown below.
The code in the policy below takes the output (response.json()
) of the XML to
JSON Outbound policy and reshapes it to a simple JSON structure.
The JSON response of your API would then be a easily consumable JSON object.
Read more about how policies work