Ravi Shankar
Ravi Shankar

Reputation: 377

How to add json object from a nested json into a db-column in Oracle DB

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.

Upvotes: 1

Views: 1430

Answers (1)

anytilia
anytilia

Reputation: 61

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;

Upvotes: 1

Related Questions