I have a json column. The object has nested fields eg
{
"logo": {
"url": "https://foo.bar"
...
}
...
}
The object has more fields, but the url
field is the one that I want to update. I believe I should use something like json_set, but I'm lost regarding the json path. Can I have an example?
You can't do that with json
only with jsonb
(but you can easily cast the existing value)
With jsonb_set
you need to provide the path to the object you want to change, so that would be '{logo,url}'
:
The following:
with t (data) as (
values
('{
"logo": { "url": "https://foo.bar", "something" : "some value"},
"other" : { "one": "two"}
}'::jsonb
)
)
select jsonb_set(data, '{logo,url}', to_jsonb('http://bar.foo'::text))
from t;
(The WITH
part is only there to generate dummy data)
returns:
jsonb_set
---------------------------------------------------------------------------------------
{"logo": {"url": "http://bar.foo", "something": "some value"}, "other": {"one": "two"}}
As you can see, only the url
attribute was replaced, everything else is left as it is.
If your column is really a json, just use your_column::jsonb
so that you can use jsonb_set()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.