Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update jsonb object in postgres

One of my column is jsonb and have value in the format. The value of a single row of column is below.

{
    "835": {
        "cost": 0, 
        "name": "FACEBOOK_FB1_6JAN2020", 
        "email": "[email protected]", 
        "views": 0, 
        "clicks": 0, 
        "impressions": 0, 
        "campaign_state": "paused", 
        "processed":"in_progress", 
        "modes":["obj1","obj2"]
    }, 
    "876": {
        "cost": 0, 
        "name": "MARVEL_BLACK_WIDOW_4DEC2019", 
        "email": "[email protected]", 
        "views": 0, 
        "clicks": 0, 
        "impressions": 0, 
        "campaign_state": "paused", 
        "processed":"in_progress", 
        "modes":["obj1","obj2"]
    }
}

I want to update campaign_info(column name) column's the inner key "processed" and "models" of the campaign_id is "876".

I have tried this query:

update safe_vid_info 
set campaign_info -> '835' --> 'processed'='completed' 
where cid = 'kiywgh'; 

But it didn't work.

Any help is appreciated. Thanks.

like image 764
Siddhant Kaushal Avatar asked Mar 08 '20 18:03

Siddhant Kaushal


1 Answers

Is this what you want?

jsonb_set(campaign_info, '{876,processed}', '"completed"')

This updates the value at path "876" > "processed" with value 'completed'.

In your update query:

update safe_vid_info 
set campaign_info = jsonb_set(campaign_info, '{876,processed}', '"completed"')
where cid = 'kiywgh'; 
like image 132
GMB Avatar answered Sep 23 '22 12:09

GMB