Skip to Main Content
  • Questions
  • XML to Json conversion through PLSQL

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question.

Asked: September 03, 2020 - 6:53 pm UTC

Last updated: August 30, 2023 - 1:18 pm UTC

Version: Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production

Viewed 10K+ times! This question is

You Asked

There is a Oracle Table which has a column which has xml data. I am planning to create another column to hold json data. Is there any way PLSQL or APEX has a function which can convert the data in xml column and place it in json in json type converted file.

and Connor said...

Check out this nice little utility on github

https://github.com/mbleron/JSONUtilities

If its fairly simple XML, you can do it with xmltable, eg

SELECT JSON_OBJECTAGG( tag VALUE val )
FROM   XMLTABLE(
         '/xml/*'
         PASSING XMLTYPE( '<xml><firstname>connor</firstname><lastname>mcdonac</lastname></xml>')
         COLUMNS 
           tag   VARCHAR2(20) PATH './name()',
           val    VARCHAR2(20) PATH './text()'
       );


Rating

  (6 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Package body is compiling with errors

Manu, September 04, 2020 - 4:07 am UTC

Thanks for reply. Pkg body is having lot of errors while compiling. Is there any specific db version this is compatible with?
Errors:
Project: C:\Users\mukidimx\AppData\Roaming\SQL Developer\system19.2.1.247.2212\o.sqldeveloper\projects\IdeConnections#PMDEV.jpr
Package Body APPS.XXLN_XUTL_JSON@PMDEV
Error(10,3): PL/SQL: Item ignored
Error(11,18): PLS-00201: identifier 'JSON_ELEMENT_T' must be declared
Error(47,17): PLS-00201: identifier 'JSON_OBJECT_T' must be declared
Error(47,17): PL/SQL: Item ignored
Error(48,17): PLS-00201: identifier 'JSON_ARRAY_T' must be declared
Error(48,17): PL/SQL: Item ignored
Error(49,17): PLS-00201: identifier 'JSON_KEY_LIST' must be declared
Error(49,17): PL/SQL: Item ignored
Error(53,5): PL/SQL: Statement ignored
Error(54,15): PLS-00320: the declaration of the type of this expression is incomplete or malformed
Error(147,16): PLS-00201: identifier 'JSON_OBJECT_T' must be declared
Error(147,16): PL/SQL: Item ignored
Error(148,16): PLS-00201: identifier 'JSON_ARRAY_T' must be declared
Error(148,16): PL/SQL: Item ignored
Error(163,9): PL/SQL: Statement ignored
Error(163,16): PLS-00320: the declaration of the type of this expression is incomplete or malformed
Error(169,11): PL/SQL: Statement ignored
Error(169,18): PLS-00320: the declaration of the type of this expression is incomplete or malformed
Error(172,13): PLS-00320: the declaration of the type of this expression is incomplete or malformed
Error(172,13): PL/SQL: Statement ignored
Warning(1,1): Only first 20 issues are reported
Connor McDonald
September 07, 2020 - 2:17 am UTC

12.2 and above.

XML to long

Teresa García, March 01, 2022 - 2:06 pm UTC

Hi, how can i resolve the error ORA-01704 if the XML that i want to convert to a JSON is longer than 4000 characters?.
Thanks
Chris Saxon
March 01, 2022 - 2:26 pm UTC

Either:

1 - Enable extended data types - this increases the max length to 32,767

https://oracle-base.com/articles/12c/extended-data-types-12cR1

2 - Use a clob to store the XML

XML with group by

Teresa García, March 14, 2022 - 3:12 pm UTC

Hi,
I have a 12 oracle BD and i can do a group by JSON with:
SELECT JSON_OBJECT('VW_SIGMA_MOTIVOS' VALUE JSON_ARRAYAGG(
JSON_OBJECT(mot.tipo_accion VALUE JSON_ARRAYAGG(
JSON_OBJECT('value' VALUE mot.TIPO_MOTIVO,
'label' VALUE mot.DESC_MOTIVO
) ORDER BY mot.DESC_MOTIVO asc RETURNING CLOB ) RETURNING CLOB ) RETURNING CLOB ) RETURNING CLOB )
FROM VW_SIGMA_MOTIVOS mot
GROUP BY mot.tipo_accion;
like this:
{
"VW_SIGMA_MOTIVOS": [
{
"ATN3": [
{
"value": "ATN3",
"label": "Investigación de accidente no traumático"
}
]
},
{
"HAB1": [
{
"value": "2017-5",
"label": "Accidentes Máquinas"
},
{
"value": "2018-5",
"label": "Accidentes Máquinas"
},
{
"value": "2017-4",
"label": "Amianto"
},
{
"value": "2019-3",
"label": "Amianto"
},
{
"value": "2021-10",
"label": "Campaña análisis ATG no investigados 2021"
}
]
},
{
"SIN": [
{
"value": "SIN",
"label": "Actuación singular"
}
]
}
]
}
I wanna do the same in a 11g BD. Is there any way to do that query in an 11 BD or to obtain a group by XML and then convert it to a JSON?. I have the XML query but i need it in an unic XML.

SELECT XMLELEMENT (tipo_accion,
XMLAGG(
XMLELEMENT("ROW",
XMLELEMENT("value", tipo_motivo),
XMLELEMENT("label", desc_motivo)
)).getClobVal()
) FROM (SELECT * FROM VW_SIGMA_MOTIVOS ORDER BY tipo_motivo) GROUP BY tipo_accion
Chris Saxon
March 15, 2022 - 1:40 pm UTC

There's no in-built syntax to generate JSON in 11g. If you've got APEX installed, you can use APEX_JSON to manage JSON documents:

https://docs.oracle.com/en/database/oracle/application-express/21.2/aeapi/APEX_JSON.html

If not, you're options are:

- Install APEX
- Get some other utility e.g. PL/JSON https://github.com/pljson/pljson
- Write the conversion/generation code yourself.

object type declarations?

Mark Tingley, August 17, 2022 - 11:43 am UTC

https://github.com/mbleron/JSONUtilities

Please provide link to type declarations. The package body won't compile without them:-
Package Body XUTL_JSON compiled

LINE/COL ERROR
--------- -------------------------------------------------------------
10/3 PL/SQL: Item ignored
11/18 PLS-00201: identifier 'JSON_ELEMENT_T' must be declared
47/17 PLS-00201: identifier 'JSON_OBJECT_T' must be declared
47/17 PL/SQL: Item ignored
48/17 PLS-00201: identifier 'JSON_ARRAY_T' must be declared
48/17 PL/SQL: Item ignored
49/17 PLS-00201: identifier 'JSON_KEY_LIST' must be declared
49/17 PL/SQL: Item ignored
53/5 PL/SQL: Statement ignored
54/15 PLS-00320: the declaration of the type of this expression is incomplete or malformed
147/16 PLS-00201: identifier 'JSON_OBJECT_T' must be declared
147/16 PL/SQL: Item ignored
148/16 PLS-00201: identifier 'JSON_ARRAY_T' must be declared
Connor McDonald
August 23, 2022 - 10:33 pm UTC

You need to be on a more recent version of Oracle - these types are native

Better an simple way to convert XML to JSON in Oracle

John Swanteson, August 28, 2023 - 1:56 pm UTC

This will convert wellformed XML to JSON:

SELECT XMLTOJSON(XMLTYPE('<xml>
<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>
</xml>')) FROM dual;
Chris Saxon
August 29, 2023 - 3:50 pm UTC

What is the definition of XMLTOJSON? There is no in-built function with that name.

xmltojson - built in function.

Rajeshwaran, Jeyabal, August 30, 2023 - 5:26 am UTC

though it is not listed here - it seems to be working.

https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/index.html#Oracle%C2%AE-Database

might be an undocumented function/feature.
demo@PDB1> SELECT XMLTOJSON(XMLTYPE('<xml>
  2  <firstName>John</firstName>
  3  <lastName>Smith</lastName>
  4  <age>25</age>
  5  <address>
  6  <streetAddress>21 2nd Street</streetAddress>
  7  <city>New York</city>
  8  <state>NY</state>
  9  <postalCode>10021</postalCode>
 10  </address>
 11  <phoneNumber>
 12  <type>home</type>
 13  <number>212 555-1234</number>
 14  </phoneNumber>
 15  <phoneNumber>
 16  <type>fax</type>
 17  <number>646 555-4567</number>
 18  </phoneNumber>
 19  </xml>')) FROM dual;

XMLTOJSON(XMLTYPE('<XML><FIRSTNAME>JOHN</FIRSTNAME><LASTNAME>SMITH</LASTNAME><AGE>25</AGE><ADDRESS><STREETADDRESS>212NDSTREET</STREETADDRESS><CITY>NEWYORK</CITY><STATE>NY</STATE><PO
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
{"xml":{"address":{"city":"New York","postalCode":10021,"state":"NY","streetAddress":"21 2nd Street"},"age":25,"firstName":"John","lastName":"Smith","phoneNumber":[{"number":"212 55
5-1234","type":"home"},{"number":"646 555-4567","type":"fax"}]}}


demo@PDB1> desc xmltojson
ERROR:
ORA-04043: object xmltojson does not exist


Chris Saxon
August 30, 2023 - 1:18 pm UTC

So it is - thanks for confirming. Not sure what the situation is with this function

More to Explore

Design

New to good database design? Check out Chris Saxon's full fundamentals class.