0

Hi i have this structure in my column jsonb in postgres

offer_profile_id (PK, FK): bigint
website_id (PK, FK):    bigint
offer_profile_website_detail: jsonb {
    display_vat_included: boolean
    available_services: [{
        service_id: long
        included_by_default: boolean
        extra_service : boolean
    }]
}

I'm trying to add a new attribute named is_active: boolean inside the available_services. The problem is that the table named "offer_profile", full of data with many rows and which can take a long time to write the insert into "offer_profile" VALUES script.

I'm looking for a dynamic way to update my table.

Regards

1 Answer 1

0

You can use jsonb_set function available in Postgresql. Here is the link. Below is the non-tested version of the code, tweak the path as needed.

UPDATE table_name
SET offer_profile_website_detail = jsonb_set(
offer_profile_website_detail, '{available_services, 0, is_active}', 'true', true)

Test:

create table test
(
    offer_profile_id             bigint,
    website_id                   bigint,
    offer_profile_website_detail jsonb
)

insert into test values (1, 1, '{"display_vat_included": true, "available_services": [{"service_id": 123}]}');
insert into test values (2, 2, '{"display_vat_included": false, "available_services": [{"service_id": 345}]}');
insert into test values (3, 3, '{"display_vat_included": false, "available_services": [{"service_id": 567}]}');

SELECT offer_profile_id, offer_profile_website_detail,
jsonb_set(offer_profile_website_detail, '{available_services, 0, is_active}', 'true', true)
from test

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your reply, I tested it but not working for me. The error is :path element at position 2 is not an integer. Can you update your comment please ?

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.