1

I trying to add new object to my table in one column called PARAM, inside of that column I have:

{"array1": [
    {"array2": [
        {"objId": 1, "data": false, "repeat": 1},
        {"objId": 2, "data": false, "repeat": 1}
    ]}
]}

I want to add new object to array2 I am using that query

UPDATE table
SET param = jsonb_set(
        param::jsonb,
        array['array1'],
        (param->'array1'->>'array2')::jsonb || jsonb_build_object('objId', '3','data', 'false','repeat', '1')::jsonb)
WHERE ...

but instead getting

{"array1": [
    {"array2": [
        {"objId": 1, "data": false, "repeat": 1},
        {"objId": 2, "data": false, "repeat": 1},
        {"objId": 3, "data": false, "repeat": 1}
    ]}
]}

my PARAM become null. Anyone can solve that?

I have based my solution on that topic

1 Answer 1

1

Try this:

select 
      jsonb_set(
                param::jsonb,
                '{array1,0,array2}',
                jsonb_extract_path(param, 'array1','0','array2')::jsonb ||
                jsonb_build_object('objId', '3','data', 'false','repeat', '1')::jsonb
               )

you can put where clause as per your requirement

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.