Convert JSON To XML
Convert JSON To XML
Convert JSON To XML
If i have
<xml><name>himasnhu</name><age>24</age></xml>
How can i covert it to
{"name":"himanshu","age":24} .
Contents
Overview
Configuration
Examples
Overview
You can use the JSON to XML filter to convert a JavaScript Object Notation (JSON)
document to an XML document. For details on the mapping conventions used, go to:
https://github.com/beckchr/staxon/wiki/Mapping-Convention
Configuration
To configure the JSON to XML filter, specify the following fields:
Name:
If the incoming JSON document has multiple root elements, enter a virtual root element to
be added to the output XML document. This is required because multiple root elements are
not valid in XML. Otherwise, the XML parser will fail. For more details, see the section called
“Examples”.
Insert processing instructions into the output XML representing JSON array
boundaries:
Select this option to enable round-trip conversion back to JSON. This inserts the necessary
processing instructions into the output XML. This option is not selected by default. For more
details, see the section called “Examples”.
Note
This option is recommended if you wish to convert back to the original JSON array
structures. This information would be lost during the translation back to XML.
Select this option to convert your JSON object names to XML element names. This option is
not selected by default.
Important
You should ensure that your JSON object names are also valid XML element names.
If this is not possible, this option analyzes each object name and automatically
performs the conversion. This has a performance overhead and is not recommended
if you wish to convert back to the original JSON.
Examples
Multiple root elements
Insert processing instructions into the output XML
For example, the following incoming JSON message has multiple root elements:
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address":
{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber":
[
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
If you enter customer in the Virtual root element field, this results in the following output
XML:
{
"customer" : {
"first-name" : "Jane",
"last-name" : "Doe",
"address" : {
"street" : "123 A Street"
},
"phone-number" : [ {
"@type" : "work",
"$" : "555-1111"
}, {
"@type" : "cell",
"$" : "555-2222"
} ]
}
}
When the Insert processing instructions into the output XML representing JSON
array boundaries option is selected, the output XML is as follows:
Overview
The Load File filter enables you to load the contents of the specified file, and set them as
message content to be processed. When the contents of the file are loaded, they can be
passed to the core message pipeline for processing by the appropriate message filters. For
example, you might use the Load File filter in cases where an external application drops
XML or JSON files on to the file system to be validated, modified, and potentially routed on
over HTTP, JMS, or stored to a directory where the application can access them again.
For example, this sort of protocol mediation can be useful when integrating legacy systems.
Instead of making drastic changes to the legacy system by adding an HTTP engine, the API
Gateway can load files from the file system, and route them on over HTTP to another back-
end system. The added benefit is that messages are exposed to the full range of message
processing filters available in the API Gateway. This ensures that only properly validated
messages are routed on to the target system.
Configuration
Input settings
Processing settings
On completion settings
Name:
Input settings
File:
Enter the name of the file to load, or browse to the file in the file system. This setting is
required.
Processing settings
The fields in this section determine what processing is performed on the input files, and
where files are placed before and after processing.
Processing Directory:
Enter or browse to the directory to which the input file is copied prior to processing. This
field is optional. If this is not specified, the input file remains in the current input directory.
Response Directory:
Enter or browse to the directory to which the response file is copied. This field is optional. If
this is not specified, the response file is not written to disk.
Processing Policy
Select the policy executed on the input file. For example, the policy could perform message
validation, routing, virus checking, or XSLT transformation. This field is optional.
File Type:
Specifies how the input file is interpreted. Select one of the following options:
Raw:
Assumes the inbound file contains an HTTP request (optionally with HTTP headers).
Use Content-type:
On completion settings
You can specify what to do when the file processing has completed. Select one of the
following options:
Do Nothing:
The input file remains in the input directory or in the Processing Directory. This is
the default.
The input file is deleted from the input directory or the Processing Directory.
Overview
Configuration
Overview
The API Gateway can strip a named HTTP header from the message as it passes through a
policy. This is especially useful in cases where end-user credentials are passed to the API
Gateway in an HTTP header. After processing the credentials, you can use the Remove
HTTP Header filter to strip the header from the message to ensure that it is not forwarded
on to the destination web service.
Configuration
To configure the Remove HTTP Header filter, perform the following steps:
Overview
Configuration
Overview
You can use the Remove XML Node filter to remove an XML element, attribute, text, or
comment node from an XML message. You can specify the node to remove using an XPath
expression. The XPath query language enables you to select nodes in an XML document.
Configuration
To configure this filter, specify the following fields:
Name:
Enter a suitable name that reflects the role of the filter. For example, if the purpose of this
filter is to remove an <ID> element from the message, it would be appropriate to name this
filter Remove ID Element.
XPath Location:
Specify an XPath expression to indicate the node to remove. When the expression is
configured correctly, you can remove an element, attribute, text, or comment node. If this
expression returns more than one node, all returned nodes are removed.
You can select XPath expressions from the list, and edit or add expressions by clicking the
relevant button. The following are some example expressions:
Text
Nodes in /soap:Envelope/soap:
soap http://schemas.xmlsoap.org/soap/envelope/
SOAP Body/text()
Body
If this option is selected, and the XPath expression returns no nodes, the filter returns false.
If this option is not selected, and the XPath returns no nodes, the filter returns true, and no
nodes are removed.
Examples
Multiple root elements
For example, the following incoming JSON message has multiple root elements:
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address":
{
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber":
[
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
If you enter customer in the Virtual root element field, this results in the following output
XML:
{
"customer" : {
"first-name" : "Jane",
"last-name" : "Doe",
"address" : {
"street" : "123 A Street"
},
"phone-number" : [ {
"@type" : "work",
"$" : "555-1111"
}, {
"@type" : "cell",
"$" : "555-2222"
} ]
}
}
When the Insert processing instructions into the output XML representing JSON
array boundaries option is selected, the output XML is as follows: