I am trying to figure out how to pull a JSON column from a table into a view and still be able to use dot-notation in the WHERE
clause.
I currently have the column defined correctly and I can use dot-notation in the WHERE
clause in a query that hits the table directly. The problem I am having is that this table is currently used in a view that I need to use to return all pertinent data.
If I simply include the column in the view definition, I lose the ability to search values in the string using dot-notation.
If I join the view back to this table in the query where I retrieve data from the view, I am able to use dot-notation in the WHERE
clause, but, I take a huge performance hit (4-5 times slower).
Here is a sample of the JSON that would be stored in each row.
{"fields":
{
"field1":{ "name": "field1","label": "My Field 1","value": "ABCD"},
"field2":{ "name": "field2","label": "My Field 2","value": ""},
"field3":{ "name": "field3","label": "My Field 3","value": "XYZ"},
"field4":{ "name": "field4","label": "My Field 4","value": ""},
"field5":{ "name": "field5","label": "My Field 5","value": ""},
"field6":{ "name": "field6","label": "My Field 6","value": "Y"},
"field7":{ "name": "field7","label": "My Field 7","value": ""}
}
}
What I would like to be able to do in the view is WHERE json_col_name.fields.field1.value = 'ABCD'
Please keep in mind that the object keys, such as field1
, field2
, etc will be arbitrary values, not always following that naming convention nor will there always be 7 fields.
I am not married to this schema and would gladly change it to get it to work.
Here is the SQL to reproduce the table/view and data.
create table MAIN_TABLE
(
ID NUMBER(10) not null
constraint MY_PK
primary key,
JSON_DATA CLOB
constraint JSON_DATA_CONST
check (JSON_DATA IS JSON)
)
INSERT INTO MAIN_TABLE (ID, JSON_DATA) VALUES (1, '{"fields": {"field1":{ "name": "field1","label": "My Field 1","value": "ABCD"},"field2":{ "name": "field2","label": "My Field 2","value": "XYZ"},"field3":{ "name": "field3","label": "My Field 3","value": "Y"},"field4":{ "name": "field4","label": "My Field 4","value": ""},"field5":{ "name": "field5","label": "My Field 5","value": ""},"field6":{ "name": "field6","label": "My Field 6","value": ""},"field7":{ "name": "field7","label": "My Field 7","value": ""},}}');
INSERT INTO MAIN_TABLE (ID, JSON_DATA) VALUES (2, '{"fields": {"field1":{ "name": "field1","label": "My Field 1","value": ""},"field2":{ "name": "field2","label": "My Field 2","value": "XYZ"},"field3":{ "name": "field3","label": "My Field 3","value": "Y"},"field4":{ "name": "field4","label": "My Field 4","value": "QWERTY"},"field5":{ "name": "field5","label": "My Field 5","value": ""},"field6":{ "name": "field6","label": "My Field 6","value": ""},"field7":{ "name": "field7","label": "My Field 7","value": ""},}}');
create or replace view JSON_TEST_VIEW as
select id, JSON_DATA
from MAIN_TABLE
union all
select id, JSON_DATA from MAIN_TABLE;
This query, that hits the table, works as expected.
select * from MAIN_TABLE m where m.json_data.fields.field1.value='ABCD'
This query, that hits the view, throws an invalid identifier
error.
select * from JSON_TEST_VIEW jtv where jtv.JSON_DATA.fields.field1.value='ABCD'
I am fairly certain that the issue stems from the UNION
as I was not initially seeing the issue in my repro case without adding it (The original view has a UNION
)