0

I am trying to parse JSON in Oracle SQL.

Oracle DB version 12.1.0.2

{
 "Rownum": "1",
 "Name": "John",
 "AddressArray":["Address1", "Address2"],
 "TextObj":[{
             "mName" : "Carol",
             "lName" : "Cena"
            },
            {
             "mName" : "Mark",
             "lName" : "Karlo"
            }
           ]
}

output should look like below:

enter image description here

6
  • You can do this using the JSON_TABLE function. What have you tried ? Commented Feb 5, 2021 at 10:59
  • I tried with JSON_TABLE but was not able to parse AddressArray. Commented Feb 5, 2021 at 11:04
  • @Shre - please edit your question to include the code from your attempt with json_table and the output or errors you got. We can then (hopefully) explain what you did wrong. Commented Feb 5, 2021 at 11:28
  • Please note there is a syntax error in the JSON. A comma is missing after ""Address2"]". So it has to be "... "Address2"], ..." Commented Feb 5, 2021 at 12:09
  • Edited my post added one more TextObj object. Commented Feb 5, 2021 at 13:18

1 Answer 1

1

I suppose "nested" will do the trick

select * from json_Table('{"Rownum": "1", "Name": "John", "AddressArray":["Address1", "Address2"], "TextObj":[{"mName" : "Carol","lName" : "Cena",}]}', '$' columns (rownr number path '$.Rownum',
                                            name varchar2(100) path '$.Name',
                                            mName varchar2(100) path '$.TextObj[*].mName',
                                            lName varchar2(100) path '$.TextObj[*].lName',
                                            nested path '$.AddressArray[*]' columns(AddressArray varchar2(100) path '$') 
                                           ));

My output:

ROWNR NAME MNAME LNAME ADDRESSARRAY
1 John Carol Cena Address1
1 John Carol Cena Address2
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @ekochergin. This don't work if TextObj has one more object like below code "TextObj":[{ "mName" : "Carol", "lName" : "Cena" }, { "mName" : "Mark", "lName" : "Karlo" } ]
@Shre, it makes TextObj an array as well. In this case you need to process it exactly as AdressArray
@Shre, sorry that make the question harder and I don't have the solution right now. Try this documentation docs.oracle.com/en/database/oracle/oracle-database/12.2/adjsn/… or submit another question. Perhaps there is someone who could help to crack multiple JSON arrays
I tried this, But not getting expected result. select * from json_Table('{ "Rownum": "1", "Name": "John", "AddressArray":["Address1", "Address2"], "TextObj":[{ "mName" : "Carol","lName" : "Cena"}, {"mName" : "Mark","lName" : "Karlo"} ] }', '$' columns ( rownr number path '$.Rownum', name varchar2(100) path '$.Name', nested path '$.TextObj[*]' columns ( mName varchar2(100) path '$.mName', lName varchar2(100) path '$.lName' ), nested path '$.AddressArray[*]' columns(AddressArray varchar2(100) path '$') ));
@Shre, it'd be better to submit another question. I'm not sure I'll be able to find the solution until next week

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.