1

If i have

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

How can i covert it to

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

Thanks.

MT0
  • 143,790
  • 11
  • 59
  • 117
Himanshu sharma
  • 7,487
  • 4
  • 42
  • 75

2 Answers2

6

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

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.


Update: Taking data from a table

SELECT JSON_OBJECTAGG( id VALUE text ) AS json
FROM   table_name t
       CROSS APPLY XMLTABLE(
         '/xml/*'
         PASSING XMLTYPE( t.xml )
         COLUMNS id   VARCHAR2(200) PATH './name()',
                 text VARCHAR2(200) PATH './text()'
       );

Which, for the sample data:

CREATE TABLE table_name(xml) AS
  SELECT '<xml><name>himasnhu</name><age>24</age></xml>' FROM DUAL;

Outputs:

JSON
{"name":"himasnhu","age":"24"}

fiddle

MT0
  • 143,790
  • 11
  • 59
  • 117
  • 1
    @edjm The fiddle shows that it works for XML with a root element and tags a single level deep; if it is not working for you then you need to provide a [MRE] and explain what the XML input is and what you expect the JSON output to be and if it does not match the OP's flat data structure then you probably need to ask a different question (because it is a different question). Just saying "it doesn't work and gives the wrong output" is not constructive because we don't know what the input is or what the expected output would be so it is impossible to help you. – MT0 May 05 '23 at 14:46
-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