'How to put array as field in json. Postgres
I have a json field in PostgreSQL:
{"cpu": 88, "hdd": 1200, "ram": 2048}.
I want to add a new array field: spec_os_max = string_to_array('RHEL 7.9,RHEL 8.*,Ubuntu 18.04,ESXi 7.0', ',')
I'm trying this:
update public.resources r
set params = jsonb_set(params::jsonb, '{spec_os_max}', string_to_array('RHEL 7.9,RHEL 8.*,Ubuntu 18.04,ESXi 7.0', ','))
where id in (122561, 122560);
I have the following errors: No function matches the given name and argument types. You might need to add explicit type casts.
Can anybody explain how to do this? Thank You.
Solution 1:[1]
As in Documentation you need to pass to jsonb_set 3 arguments : target jsonb, path text[] and new value as jsonb. You can made it like this:
with resources as (
select '{"abc": 1, "def": "some text"}'::jsonb as params
)
select jsonb_set(params, array['spec_os_max'], array_to_json(string_to_array('RHEL 7.9,RHEL 8.*,Ubuntu 18.04,ESXi 7.0', ','))::jsonb)
from resources
Solution 2:[2]
update t
set properties = jsonb_set(properties, '{spec}', jsonb_build_array('aa', 'bb'));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrew |
| Solution 2 | Vesa Karjalainen |
