I'm trying to get Oracle to produce JSON null values on SQL NULL data, as follows:
select
json_object(key 'a' value 1, key 'b' value null null on null) c1,
json_object(key 'a' value 1, key 'b' value null absent on null) c2
from dual;
Or also:
select
json_object(key 'a' value a, key 'b' value b null on null) c1,
json_object(key 'a' value a, key 'b' value b absent on null) c2
from (
select 1 a, null b
from dual
) t;
Unfortunately, both queries result in:
|C1 |C2 |
|----------|----------|
|{"a":1} |{"a":1} |
I would have expected this, instead:
|C1 |C2 |
|-------------------|----------|
|{"a":1,"b":null} |{"a":1} |
What am I missing? I'm using Oracle XE 18c
There appears to be a bug on how Oracle handles JSON_OBJECT and it will take the parameter from the last JSON_on_null_clause in the statement and apply it to all of the JSON_OBJECT expressions:
CREATE TABLE t ( a,b ) AS
SELECT 1, null FROM DUAL UNION ALL
SELECT 2, 'bb' FROM DUAL;
If you do this:
select json_object(
KEY 'a' VALUE a,
KEY 'b' VALUE b null on null
) c1,
json_object(
KEY 'a' VALUE a,
KEY 'b' VALUE b absent on null
) c2
from t;
Then the output is:
C1 | C2
:--------------- | :---------------
{"a":1} | {"a":1}
{"a":2,"b":"bb"} | {"a":2,"b":"bb"}
If you perform the same query with the expressions reversed:
select json_object(
KEY 'a' VALUE a,
KEY 'b' VALUE b absent on null
) c2,
json_object(
KEY 'a' VALUE a,
KEY 'b' VALUE b null on null
) c1
from t;
Then the output is:
C2 | C1
:--------------- | :---------------
{"a":1,"b":null} | {"a":1,"b":null}
{"a":2,"b":"bb"} | {"a":2,"b":"bb"}
db<>fiddle here
you found a bug. I have filed Bug 31013529 - TWO JSON_OBJECT WITH DIFFERENT ON NULL HANDLER RETURN WRONG RESULTS
We'll fix it asap and include it in the bundle patches. Let us know if you need a one off patch
thanks b
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With