5

I have jsonb column like so:

{name: "Toby", occupation: "Software Engineer", interests: ""}

Now, I need to update the row and put a text array like ['Volleyball', 'Football', 'Swim'] into interests field. What I've tried so far:

UPDATE users SET data = jsonb_set(data, '{interests}', ARRAY['Volleyball', 'Football', 'Swim'], true) WHERE id=84;

data is the jsonb column

But it returns an error:

ERROR: function jsonb_set(jsonb, unknown, integer[], boolean) does not exist

Hint: No function matches the given name and argument types. You might need to add explicit type casts.

P.S:

I'm using PostgreSQL 10

1 Answer 1

8

The third argument needs to be of JSONB type too.

UPDATE users SET data = jsonb_set(data, '{interests}', '["Volleyball", "Football", "Swim"]'::jsonb, true) WHERE id=84;

This will also work, which is a little closer to your example using ARRAY:

UPDATE users SET data = jsonb_set(data, '{interests}', to_jsonb(array['Volleyball', 'Football', 'Swim']), true) WHERE id=84
Sign up to request clarification or add additional context in comments.

1 Comment

In case this post comes up as a search result for anyone else, the error in my case was that I was trying to build the path (2nd argument) dynamically, using a column from another table. My mistake was not converting it to a TEXT array (::TEXT[]).

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.