i'm dealing with a column of a postgresql table containing jsons having this kind of structure:
{
"id": "a",
"user_id": " e",
"event_id": 1,
"last_snooze_timestamp": "2021-02-25T13:45:26.000000+00:00",
"number_of_participants": 3,
"participants": {
"743d774d-835a-436a-b7e8-0acb6af9f683":{
"nome": "abc",
"cognome": "abc",
"pdfURL": "indirizzoPDF",
"type": "Booker",
"access": null
},
"453f0613-e1fb-41ef-bf35-5e0520ed8995": {
"nome": "cde",
"cognome": "cde",
"pdfURL": "indirizzoPDF",
"type": "Minor",
"access": null
}
}
}
My task is to update the value of the key "access" with the current timestamp, if and only if the previous value is null. My function obviously get the id ("743d774d-835a-436a-b7e8-0acb6af9f683", "453f0613-e1fb-41ef-bf35-5e0520ed8995", etc) as input and i'm able to reach the interesting row of the table.
I've tried this kind of syntax:
SELECT jsonb_set(json_to_modify, 'path', jsonb '{"key":value}')in which I was thinking about overwriting
{"access": null}with{"access": timestamp}, but:- I cannot point correctly at the
participantsjsonb, - overwriting the whole key/value pair feels inelegant and somewhat dangerous.
- I cannot point correctly at the
the syntax that I found in this page: https://dev.to/deepika_banoth/how-to-use-jsonbset-function-in-postgresql-35eo
at the point 2:
UPDATE "json" SET "participants"=jsonb_set("participants"::jsonb, '{access}', '"timestamp"' WHERE "details"::json->>'id'='"743d774d-835a-436a-b7e8-0acb6af9f683"'but still cannot make it work because i'm not able to correctly point at the participants json.
- other pointless syntaxes.
I will be grateful to anyone who wants to help me or give me clues on how to deal with the problem.