1

I'm trying to update a value in a jsonb column in postgresql.

// column 'settings'
{
  "country": "us",
  "language": "en"
}

I want to use prepared statements and I want to update a specific value, so not the whole jsonb object.

This does not work:

UPDATE users SET settings = jsonb_set(settings, '{country}', '$1'::jsonb)

I'm trying to cast the value of the prepared statement to jsonb, it does not work. Error is invalid input syntax for type json.

What is the correct way to update specific values in a jsonb field.

I'm using nodejs pg.

1 Answer 1

3

I think the following syntax should work in a prepared statement

UPDATE users SET settings = jsonb_set(settings, '{country}', to_jsonb($1::text))
Sign up to request clarification or add additional context in comments.

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.