I have a jsonb column within my table that looks like this:
[
{
"id: "1231-213-331",
"name" : "Object 1 Name"
},
{
"id: "4113-13-131-21",
"name" : "Object 2 Name"
}
]
I am passing a json
object into my postgresql function:
{
"id: "1231-213-331",
"name" : "New Name For Object One"
}
With this, I would like to entirely replace the existing json object within the jsonb object with a matching id (which is unique).
I’ve tried concatenation via ||
and using the jsonb_set
function, which apparently doesn’t exist (I’m using postgres 15.4)
How can this be done?
2
Answers
I would find the match, turn it into a
text[]
, and then userjsonb_set()
to perform the replacement:working fiddle
Your error is because function parameters must be
jsonb_set ( target jsonb, path text[], new_value jsonb [, create_if_missing boolean ] )
. Simulating your function with an anonymous block it would be something like thisFiddle to test