1

If i have

<xml><name>himasnhu</name><age>24</age></xml>  

How can i covert it to

{"name":"himanshu","age":24} .

Thanks.

MT0
  • 113,669
  • 10
  • 50
  • 103
Himanshu sharma
  • 6,884
  • 4
  • 40
  • 71

2 Answers2

5

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()'
       );

I'm not on a 12c system so this is untested.

In earlier versions you can write a Java function [1] [2] using one of the many Java JSON packages to perform the conversion and then load it into the database using the loadjava utility (or a CREATE JAVA statement) and then use that.

MT0
  • 113,669
  • 10
  • 50
  • 103
-3

You can use the XML to JSON filter to convert an XML document to a JavaScript Object Notation (JSON) document. For details on the mapping conventions used, see:Github- Mapping convention

Configuration

To configure the XML to JSON filter, specify the following fields:

Name:

Enter a suitable name to reflect the role of this filter.

Automatically insert JSON array boundaries:

Select this option to attempt to automatically reconstruct JSON arrays from the incoming XML document. This option is selected by default.

[Note] If the incoming XML document includes the processing instruction, the JSON array is reconstructed regardless of this option setting. If the XML document does not contain , and this option is selected, the filter makes an attempt at guessing what should be part of the array by examining the element names.

  • 1
    The OP is asking about this in an Oracle database - you should expand your answer to show the steps and code necessary to run this within the database (i.e. using the `loadjava` utility and `CREATE JAVA` statement to load the Jar and the custom Java function and then `CREATE FUNCTION .. AS LANGUAGE JAVA ...` to create an Oracle function to call it) and provide an example of how it would be used. At the moment this is a Java answer to an Oracle question so is not very useful (and is mostly just a link to an off-site resource rather than containing the necessary code in the answer). – MT0 Jun 21 '17 at 11:00