API Management
13.2 13.1 13.0 12.2 12.1 12.0 11.0
13.2 13.1 13.0 12.2 12.1 12.0 11.0

XML to JSON

This policy allows a user to convert the request/response from the XML format to the JSON format.

The usage of JSON format has been increasing for serializing and transmitting structured data over a network connection such as data transmission between a server and a web application. Since JSON format is not widely adopted by all organizations, and as most of the data exchange is done through XML in earlier networks that have not deployed JSON, conversion from JSON to XML or vice-versa has become essential for data interoperability. The policy serves the need to convert data from the XML format to the JSON format.

Configuration

The properties that have to be configured to use the policy are described below.

image2021-12-2 11:23:14.png
Figure 1: XML to JSON Policy Configuration attributes

Property

Description

Skip NameSpaces

Whether or not to skip adding namespace declarations to XML elements while converting.

Remove NameSpace Prefix from Elements

Whether or not the namespace prefix from XML elements should be removed when reading for XMLSerializer.

Skip root element

Whether to skip Root Element while converting from XML to JSON. Determines whether the resulting JSON will start off with a top-most element whose name matches the XML root element.

Example

Consider the XML string below:

<a><x>1</x><y>2</y></a>

If the property is

  • enabled, the string is translated as:

    {"x": "1", "y": "2"}
    


  • disabled, the string is translated as:

    { "a":  { "x": "1", "y": "2" }}
    



Indent Factor

String Indent value for JSON Object.

Example

Sample Input

JavaScript
<ns1:BookStore xmlns:ns1="http://www.books.org">
   <ns1:Book>
      <ns1:Title>Title</ns1:Title>
      <ns1:Author>Author</ns1:Author>
      <ns1:Date>Date</ns1:Date>
      <ns1:ISBN>ISBN</ns1:ISBN>
      <ns1:Publisher>Publisher</ns1:Publisher>
   </ns1:Book>
</ns1:BookStore>

Output

Various outputs depending on changes in the configuration are given below.

JavaScript
{"BookStore":    {
       "@xmlns:ns1": "http://www.books.org",
       "Book":        {
           "Title": "Title",
           "Author": "Author",
           "Date": "Date",
           "ISBN": "ISBN",
           "Publisher": "Publisher"
       }
   }}


JavaScript
{"BookStore": {"Book":    {
       "Title": "Title",
       "Author": "Author",
       "Date": "Date",
       "ISBN": "ISBN",
       "Publisher": "Publisher"
   }}} 


JavaScript
{"ns1:BookStore": {"ns1:Book":    {
       "ns1:Title": "Title",
       "ns1:Author": "Author",
       "ns1:Date": "Date",
       "ns1:ISBN": "ISBN",
       "ns1:Publisher": "Publisher"
   }}}