Skip to Main Content
  • Questions
  • Oracle JSON_OBJECT_T parse fails with ORA-40441 when input has special characters

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Andres.

Asked: June 27, 2019 - 5:05 pm UTC

Last updated: November 04, 2019 - 1:12 pm UTC

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

Viewed 10K+ times! This question is

You Asked

Hi Tom, I´m trying to parse a Json.
I´m using Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production

The Json input is CLOB because it can have more than 32767 characters.

Some of the values can be special characters and parse fails when this characters are present.


Here are two very simple examples

/*WORKS FINE*/
DECLARE
    L_datos    CLOB;
    L_json    Json_object_t;
BEGIN
    l_datos:='{value:"hola"}';
    L_json := Json_object_t.Parse (L_datos);
END;

/*FAILS BECAUSE THE character "á"*/
DECLARE
    L_datos    CLOB;
    L_json    Json_object_t;
BEGIN
    l_datos:='{value:"holá"}';
    L_json := Json_object_t.Parse (L_datos);
END;


Fail message:

Error at line 12
ORA-40441: Error de sintaxis de JSON
ORA-06512: en "SYS.JDOM_T", línea 9
ORA-06512: en "SYS.JSON_OBJECT_T", línea 100
ORA-06512: en línea 7


¿How can I avoid this problem?

Thanks!

and Chris said...

The issue is resolved in 18c. So you could upgrade to that ;)

Or you could try storing the characters as Unicode:

DECLARE
    L_datos    CLOB;
    L_json    Json_object_t;
BEGIN
    l_datos:='{value:"hol\u00E1"}';
    L_json := Json_object_t.Parse (L_datos);
END;
/

Rating

  (5 ratings)

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

Comments

Thanks a lot!

Andres R Barcia, October 02, 2019 - 7:31 pm UTC

Ok, thanks, I'll wait for the DBA's to perform the upgrade... a couple of years.
in the meanwhile i'll try storing the characters as Unicode.

Clarification

Vic, October 04, 2019 - 10:28 am UTC

Are you saying that parsing special characters will work with json functions in an oracle 18+ database regardless of the characterset. If I upgrade my 1252 database to oracle 18, then the json functions will work?
Chris Saxon
October 04, 2019 - 12:17 pm UTC

I'm not sure all special characters will work, but accented characters work fine in 18c for me.

re: Clarification

Stew Ashton, October 04, 2019 - 2:53 pm UTC

In 19c with a database character set of AL32UTF8, Json_object_t.Parse works with the first 1900 unicode characters. This includes any characters that require escaping.
DECLARE
    L_datos    CLOB;
    L_json    Json_object_t;
BEGIN
    select json_object('value' is listagg(unistr('\'||to_char(level-1,'fm000X'))) returning clob)
    into l_datos
    from dual
    connect by level <= 1900;
    L_json := Json_object_t.Parse (L_datos);
END;
/
Best regards,
Stew Ashton

Update

Vic, November 04, 2019 - 11:52 am UTC

I tested an indeed the issue is fixed in 19

SQL*Plus: Release 19.0.0.0.0 Version 19.5.0.0.0

SQL> SELECT value$ FROM sys.props$ WHERE name = 'NLS_CHARACTERSET' ;

VALUE$
--------------------------------------------------------------------------------
WE8MSWIN1252

SQL> declare
2
3 l_json_obj JSON_OBJECT_T;

4 5 begin
6
l_json_obj := JSON_OBJECT_T.parse(' { "MedicalConditionDiagnosis": "Sclérose latérale amyotrophique (maladie de Lou-Gehrig)" } ');

7 8 9 DBMS_OUTPUT.put_line('l_json_obj ' || l_json_obj.TO_STRING);
10
end; 11
12 /

PL/SQL procedure successfully completed.



Chris Saxon
November 04, 2019 - 1:12 pm UTC

Thanks for confirming

Workaround

Vic, November 04, 2019 - 11:54 am UTC

convert function...until you upgrade.
That is what we are doing.

declare
l_json_obj JSON_OBJECT_T;
begin
l_json_obj := JSON_OBJECT_T.parse(convert('{ "MedicalConditionDiagnosis": "Sclérose latérale amyotrophique (maladie de Lou-Gehrig)" } ','AL32UTF8','WE8MSWIN1252'));
DBMS_OUTPUT.put_line('l_json_obj ' || l_json_obj.TO_STRING);
DBMS_OUTPUT.put_line('l_json_obj ' || l_json_obj.get_string('MedicalConditionDiagnosis'));

end;


Chris Saxon
November 04, 2019 - 1:12 pm UTC

Thanks for sharing.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library