0

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.

1
  • You are mixing up PostgreSQL arrays and JSON arrays. Commented Feb 25, 2022 at 13:16

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

1
update t 
set properties = jsonb_set(properties, '{spec}', jsonb_build_array('aa', 'bb'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.