Home > Blockchain >  Add jsonb property of type boolean
Add jsonb property of type boolean

Time:10-07

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

CodePudding user response:

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);

CodePudding user response:

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.

  • Related