2

I have the following json:

{
    "title": "title",
    "description": "description",
    "references": [{
        "reference_id": 181
    },
{
        "reference_id": 182
    }]
}

I want to insert the data of references array in a table xyz which contains the following columns: buffer_id and reference_id

INSERT INTO xyz (
        buffer_id,
        reference_id
    )
        SELECT
            l_buffer_id,
            reference_id
        FROM
            dual,
            JSON_TABLE ( data, '$.references[*]'
                    COLUMNS (
                        l_buffer_id ,
                        reference_id NUMBER PATH '$.reference_id'
                    )
                );

Here, l_buffer_id is a variable where my value is stored. I tried the above method to insert data into xyz. However it is not working. Is there any way to get this to work?

1 Answer 1

1

You can use such a mechanism :

INSERT INTO xyz (
        buffer_id,
        reference_id
    )
WITH tab(data) AS
(
 SELECT '{"title": "title",
    "description": "description",
    "references": [{
        "reference_id": 181
    },
{
        "reference_id": 182
    }]
}' FROM dual
)
SELECT row_number() over (order by reference_id) as l_buffer_id,
       reference_id
  FROM tab,
       json_table(data, '$'
               COLUMNS (NESTED PATH '$."references"[*]'
                   COLUMNS (reference_id NUMBER PATH '$."reference_id"')));  

Demo

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.