1

I need to insert json from a nested json file into a column in Oracle. For example, in the following json

{
"name":"John",
"age":30,
"cars": {
    "car1":"Ford",
    "car2":"BMW",
    "car3":"Fiat"
}
}

I need to store the whole json:

"cars": {
"car1":"Ford",
"car2":"BMW",
"car3":"Fiat"
}

in a db column. How can i do that? I am using Oracle DB.


I have tried the following query but its not working. (Says clob isn't a valid datatype)

select x.*
    from json_tab t,
             json_table (t.json_data, '$.[*]' 
         COLUMNS
            name  VARCHAR2(4000) PATH '$.name',
            cars clob PATH '$.cars[*]') x;

I have tried the same using varchar2 datatype but it selects null.

1 Answer 1

1

Assuming you use oracle 12c, try to experiment with this query to achieve the result you need.

select x.*, json_object(key 'cars' value x.cars format json) cars_json 
    from json_tab t, 
        json_table(t.json_data, '$'
                 COLUMNS
                   name  VARCHAR2(4000) PATH '$.name',
                   cars VARCHAR(4000) format json PATH '$.cars[*]') as x;
Sign up to request clarification or add additional context in comments.

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.