0

Newer to working with JSON and newly upgraded Oracle 19c DB. I'm receiving a JSON array back from an api and storing it in an Oracle 19c table column with IS JSON (STRICT) constraint.

[ {"key1":"valueA", "key2":"valueB"}, {"key1":"valueC", "key2":"valueD"} ]

I need to select values in column form:

KEY1    KEY2
valueA  valueB
valueC  valueD

This returns one row with null columns.

Select jt.* 
  From json_data, 
       json_table(myData, '$.[*]' 
       columns( key1, key2)) jt;

I can't seem to make the Oracle functions (json_table, json_query, json_value, ...) handle this without wrapping the array in an object.

{ "base":[ {"key1":"valueA", "key2":"valueB"}, {"key1":"valueC", "key2":"valueD"} ] }

Then this query works:

Select jt.* 
  From json_data, 
       json_table(myData, '$.base[*]' 
       columns( key1, key2)) jt;

Is there a shortcoming with the Oracle functions or what am I doing wrong?

3 Answers 3

2
Select jt.* 
  From json_data, 
       json_table(myData, '$[*]' 
       columns( key1, key2)) jt;

Full test case with results:

with json_data(myData) as (
select '[ {"key1":"valueA", "key2":"valueB"}, {"key1":"valueC", "key2":"valueD"} ]' from dual
)
Select jt.* 
  From json_data, 
       json_table(myData, '$[*]' 
       columns( key1, key2)) jt;

KEY1                 KEY2
-------------------- --------------------
valueA               valueB
valueC               valueD
Sign up to request clarification or add additional context in comments.

Comments

0

You want $[*] not $.[*]

SELECT jt.* 
FROM   json_data
       CROSS APPLY json_table(
         myData,
         '$[*]' 
         columns(
           key1,
           key2
         )
       ) jt;

Which for the sample data:

CREATE TABLE json_data ( myData VARCHAR2(2000) CHECK( myData IS JSON(STRICT) ) );

INSERT INTO json_data ( myData )
VALUES ( '[ {"key1":"valueA", "key2":"valueB"}, {"key1":"valueC", "key2":"valueD"} ]' );

Outputs:

KEY1 KEY2
valueA valueB
valueC valueD

db<>fiddle here

Comments

0

Simplified:

SELECT *
FROM json_table(
    (SELECT '[ {"key1":"valueA", "key2":"valueB"}, {"key1":"valueC", "key2":"valueD"} ]' FROM dual),
'$[*]' COLUMNS(key1, key2));

Comments

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.