JSON_EQUAL function in Oracle 18c

Oracle introduced the JSON_EQUAL function in 18c for matching the content stored in the database in JSON format. JSON_EQUAL works regardless of the order in which data is present. It will help a lot during matching purposes.

Example of using JSON_EQUAL function as:

-- Create first table and insert data into the table

create table json_test ( id number, data clob );

insert into json_test values (101,'{region_id: 1, region_name: "Europe"}');
insert into json_test values (102,'{region_id: 2, region_name: "Americas"}');
insert into json_test values (103,'{region_id: 3}');
insert into json_test values (105,'{}');

-- Create second table
create table json_test2 ( id2 number, data2 clob);

insert into json_test2 values (101,'{region_id: 1, region_name: "Europe"}');
insert into json_test2 values (102,'{region_name: "Americas",region_id: 2}');
insert into json_test2 values (103,'{region_id: 4}');
insert into json_test2 values (105,'{}');


--Compare the tables with simple equal to to see the difference
set line 999
col id for 999
col data for a40
col data2 for a40
col id2 for 999
select * from json_test a, json_test2 b where to_char(a.data) = to_char(b.data2)
  ID DATA                                      ID2 DATA2
---- ---------------------------------------- ---- ----------------------------------------
 101 {region_id: 1, region_name: "Europe"}     101 {region_id: 1, region_name: "Europe"}
 105 {}                                        105 {}

--See the difference by using the JSON_EQUAL number
  ID DATA                                      ID2 DATA2
---- ---------------------------------------- ---- ----------------------------------------
 101 {region_id: 1, region_name: "Europe"}     101 {region_id: 1, region_name: "Europe"}
 102 {region_id: 2, region_name: "Americas"}   102 {region_name: "Americas",region_id: 2}
 105 {}                                        105 {}


Note: As you see it skip the data with simple equal operator thats why we need JSON_EQUAL function

USE of Not JSON_EQUAL as mentioned below:

set line 999
col id for 999
col data for a40
col data2 for a40
col id2 for 999
select * from json_test a, json_test2 b where NOT JSON_EQUAL(a.data,b.data2);

  ID DATA                                      ID2 DATA2
---- ---------------------------------------- ---- ----------------------------------------
 101 {region_id: 1, region_name: "Europe"}     102 {region_name: "Americas",region_id: 2}
 101 {region_id: 1, region_name: "Europe"}     103 {region_id: 4}
 101 {region_id: 1, region_name: "Europe"}     105 {}
 102 {region_id: 2, region_name: "Americas"}   101 {region_id: 1, region_name: "Europe"}
 102 {region_id: 2, region_name: "Americas"}   103 {region_id: 4}
 102 {region_id: 2, region_name: "Americas"}   105 {}
 103 {region_id: 3}                            101 {region_id: 1, region_name: "Europe"}
 103 {region_id: 3}                            102 {region_name: "Americas",region_id: 2}
 103 {region_id: 3}                            103 {region_id: 4}
 103 {region_id: 3}                            105 {}
 105 {}                                        101 {region_id: 1, region_name: "Europe"}
 105 {}                                        102 {region_name: "Americas",region_id: 2}
 105 {}                                        103 {region_id: 4}
This entry was posted in Oracle on by .
Unknown's avatar

About SandeepSingh

Hi, I am working in IT industry with having more than 15 year of experience, worked as an Oracle DBA with a Company and handling different databases like Oracle, SQL Server , DB2 etc Worked as a Development and Database Administrator.

Leave a Reply