Cheat Sheet Postgresql Json
Cheat Sheet Postgresql Json
www.databasestar.com
JSON Example Selecting Updating
{ Update field by concatenating:
“color”: “black”, Select with key and value:
“drawers”: [ (displays a value such as "blue" with surrounding quotes) UPDATE product
{ SET attributes =
SELECT attributes || '{"width":"100cm"}'
“side”: “left”,
id, WHERE id = 1;
“height”: “30cm”
product_name,
},
attributes -> 'color' AS color_key Update field using jSONB_SET:
{
FROM product;
“side”: “left”,
UPDATE product
“height”: “40cm”
Select with key and value: SET attributes =
}
(displays a value such as "blue" without surrounding quotes) JSONB_SET(attributes, '{height}', '"75cm"')
],
WHERE id = 1;
“material”: “metal” SELECT
} id,
product_name,
Deleting
Data Types attributes ->> 'color' AS color_key
FROM product;
Delete based on filter:
JSON: regular JSON
Select an array value with key and value: DELETE FROM product
JSONB: JSON Binary. The recommended data type. WHERE attributes ->> 'color' = 'brown';
SELECT Remove attribute from field:
id,
Creating a JSON Field product_name,
attributes -> 'drawers' -> 1 AS drawer_value
UPDATE product
SET attributes = attributes - 'height'
FROM product; WHERE id = 1;
Create Table with JSONB field: