7

is it possible to define the path expression in JSON_QUERY dynamically based on variables?

DECLARE
    varpath varchar(180):= 'testvar1.testattribute';
BEGIN
    SELECT (JSON_QUERY(json_field FORMAT JSON , '$.' || varpath RETURNING VARCHAR2(4000) ASIS  WITHOUT ARRAY WRAPPER NULL ON ERROR)) FROM example1; 
END;

The concatenation of '$.' || varpath leads to the error:

PL/SQL: ORA-40454: path expression not a literal.

Is there any way to achieve this? In my use case, I need to fetch the path dynamically from other tables, that's why I built this short test script.

Thanks in advance!

1 Answer 1

4

You can use EXECUTE IMMEDIATE to solve it.

DECLARE
    varpath varchar2(180):= 'testvar1.testattribute';
    v_query_str varchar2(4000);
    v_result varchar2(4000);
BEGIN
    v_query_str := 'SELECT JSON_QUERY(json_field FORMAT JSON , ''$.' || varpath || ''' RETURNING VARCHAR2(4000) ASIS  WITHOUT ARRAY WRAPPER NULL ON ERROR) FROM example1'; 
    EXECUTE IMMEDIATE v_query_str INTO v_result;
END;
Sign up to request clarification or add additional context in comments.

1 Comment

Holy cow. Oracle really knows how to confuse and annoy programmers. But this does in fact work for me.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.