Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Convert JSON To XML

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

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} .

In Oracle 12.2 you should be able to use:

SELECT JSON_OBJECTAGG( id VALUE text )


FROM XMLTABLE(
'/xml/*'
PASSING XMLTYPE( '<xml><name>himanshu</name></xml>')
COLUMNS id VARCHAR2(200) PATH './name()',
text VARCHAR2(200) PATH './text()'
);

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:

Enter a suitable name that reflects the role of the filter.

Virtual root element:

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.

For more details, see the Convert XML to JSON topic.

Convert JSON object names to valid XML element names:

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

This section shows examples of using JSON to XML filter options.

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:

<?xml version="1.0" encoding="utf-8"?>


<customer>
<firstName>John</firstName>
<lastName>Smith</lastName>
<age>25</age>
<address>
<streetAddress>21 2nd Street</streetAddress>
<city>New York</city>
<state>NY</state>
<postalCode>10021</postalCode>
</address>
<phoneNumber>
<type>home</type>
<number>212 555-1234</number>
</phoneNumber>
<phoneNumber>
<type>fax</type>
<number>646 555-4567</number>
</phoneNumber>
</customer>

Insert processing instructions into the output XML

For example, take the following incoming JSON message:

{
"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:

<?xml version="1.0" encoding="utf-8"?>


<customer>
<first-name>Jane</first-name>
<last-name>Doe</last-name>
<address>
<street>123 A Street</street>
</address>
<?xml-multiple phone-number?>
<phone-number type="work">555-1111</phone-number>
<phone-number type="cell">555-2222</phone-number>
</customer>

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

Configure the following fields:

Name:

Enter an appropriate name for the filter.

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 a content-type of application/octet-stream. This is the default.

 Treat as HTTP Message (including headers):

Assumes the inbound file contains an HTTP request (optionally with HTTP headers).

 Infer content-type from extension:

Performs a lookup on configured MIME/DIME types to determine the content-type of


the file based on its extension.

 Use Content-type:

Enables you to specify a content-type in the textbox.

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.

 Delete Input File:

The input file is deleted from the input directory or the Processing Directory.

 Move Input File:

The input file is moved (archived) to the directory specified in the To


Directory field. You can also specify an optional File Prefix or File Suffix for the
archived file.

Remove HTTP header


Contents

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:

1. Enter an appropriate name for this filter in the Name field.


2. Specify name of the HTTP header to remove in the HTTP Header Name field.
3. Select the Fail if header is not present check box to configure the API Gateway to
abort the filter if the message does not contain the named HTTP header. Headers can
be added to the message using the Add HTTP header filter.

Remove XML node


Contents

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:

Name XPath Expression Prefix URI

The First http://docs.oasis-open.org/wss/2004/01/


WSSE
//wsse:Security[1] wsse oasis-200401-wss-wssecurity-secext-
Security
1.0.xsd
element

Text
Nodes in /soap:Envelope/soap:
soap http://schemas.xmlsoap.org/soap/envelope/
SOAP Body/text()
Body

Fail if no nodes returned from XPath:

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.

Save deleted nodes to be reinserted to new location:


You can use this option in cases where you want to move XML nodes from one location in
the message to another. By selecting this option, the deleted nodes are stored in
the deleted.node.list message attribute. You can then use the Add XML Node filter to
insert the deleted nodes back into a different location in the message. For more details, see
the Add XML node topic.

Examples
Multiple root elements

Insert processing instructions into the output XML

This section shows examples of using JSON to XML filter options.

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:

<?xml version="1.0" encoding="utf-8"?>


<customer>
<firstName>John</firstName>
<lastName>Smith</lastName>
<age>25</age>
<address>
<streetAddress>21 2nd Street</streetAddress>
<city>New York</city>
<state>NY</state>
<postalCode>10021</postalCode>
</address>
<phoneNumber>
<type>home</type>
<number>212 555-1234</number>
</phoneNumber>
<phoneNumber>
<type>fax</type>
<number>646 555-4567</number>
</phoneNumber>
</customer>

Insert processing instructions into the output XML

For example, take the following incoming JSON message:

{
"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:

<?xml version="1.0" encoding="utf-8"?>


<customer>
<first-name>Jane</first-name>
<last-name>Doe</last-name>
<address>
<street>123 A Street</street>
</address>
<?xml-multiple phone-number?>
<phone-number type="work">555-1111</phone-number>
<phone-number type="cell">555-2222</phone-number>
</customer>

You might also like