Alexander Zeitler
Alexander Zeitler

Reputation: 13099

Add jsonb property of type boolean

I'm trying to add a property to an existing jsonb column (column "data").

I want have my jsonb document to log like this

{
  // ... existing properties
  "Filed": false // new property
}

I tried

UPDATE "doc" SET "data" = jsonb_set("data"::jsonb, 'Filed', false, true)

I get this error:

[42883] ERROR: function jsonb_set(jsonb, unknown, boolean, boolean) does not exist 
Hint: No function matches the given name and argument types. 
You might need to add explicit type casts. Position: 46

Upvotes: 0

Views: 235

Answers (2)

Laurenz Albe
Laurenz Albe

Reputation: 246788

It should be

jsonb_set("data"::jsonb, '{Filed}', 'false', TRUE)

The second parameter is an array denoting the path to the appropriate key, and 'false' is the string representation of a JSON boolean.

Upvotes: 1

Stefanov.sm
Stefanov.sm

Reputation: 13049

Better use the || operator.

UPDATE "doc" SET "data" = "data" || '{"Filed": false}';

This one is equivalent but more suitable for parameterization:

UPDATE "doc" SET "data" = "data" || jsonb_build_object('Filed', false);

Upvotes: 0

Related Questions