0

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)

4
  • 1
    Please provide a minimal reproducible example of the problem.
    – GMB
    Jul 17, 2020 at 17:12
  • 1
    Can you give an example of the query you are using, both against the table and against the view, along with the definitions of the table and the view?
    – pmdba
    Jul 17, 2020 at 17:13
  • I am working on simplifying the table and view definitions. Jul 17, 2020 at 18:09
  • @pmdba I have updated the question with the necessary SQL to reproduce the issue. Jul 17, 2020 at 19:38

1 Answer 1

3

Dot notation is only valid for JSON that has tagged with the "IS JSON" check constraint.

On your table, we know that the data is JSON, but once you put it into a view, then we no longer have that confidence. After all you could have done.

create view V as
select [some json]
union all
select [some junk]

So in this case, you'll need to let us know that the JSON can indeed be treated as such.

SQL> create or replace view JSON_TEST_VIEW as
  2  select m.id,  treat(m.json_data as json) JSON_DATA
  3  from
  4  (
  5    select id, JSON_DATA
  6    from MAIN_TABLE
  7    union all
  8    select id, JSON_DATA from MAIN_TABLE
  9  ) m;

View created.

SQL>
SQL> select * from JSON_TEST_VIEW jtv where jtv.JSON_DATA.fields.field1.value='ABCD';

        ID JSON_DATA
---------- --------------------------------------------------------------------------------
         1 {"fields": {"field1":{ "name": "field1","label": "My Field 1","value": "ABCD"},"
           field2":{ "name": "field2","label": "My Field 2","value": "XYZ"},"field3":{ "nam
           e": "field3","label": "My Field 3","value": "Y"},"field4":{ "name": "field4","la
           bel": "My Field 4","value": ""},"field5":{ "name": "field5","label": "My Field 5
           ","value": ""},"field6":{ "name": "field6","label": "My Field 6","value": ""},"f
           ield7":{ "name": "field7","label": "My Field 7","value": ""},}}

         1 {"fields": {"field1":{ "name": "field1","label": "My Field 1","value": "ABCD"},"
           field2":{ "name": "field2","label": "My Field 2","value": "XYZ"},"field3":{ "nam
           e": "field3","label": "My Field 3","value": "Y"},"field4":{ "name": "field4","la
           bel": "My Field 4","value": ""},"field5":{ "name": "field5","label": "My Field 5
           ","value": ""},"field6":{ "name": "field6","label": "My Field 6","value": ""},"f
           ield7":{ "name": "field7","label": "My Field 7","value": ""},}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.