Оператор ->>
возвращает значение по ключу как текст:
SELECT name, preferences->>'theme' AS theme
FROM users;
name | theme |
---|---|
Alexander | dark |
Vladislav | light |
Оператор @>
проверяет, содержит ли JSON другой JSON:
SELECT * FROM users
WHERE preferences @> '{"theme": "dark"}';
id | name | preferences |
---|---|---|
7 | Alexander | {"theme": "dark", "notifications": true} |
Функция jsonb_set
обновляет значение в JSON:
UPDATE users
SET preferences = jsonb_set(preferences, '{theme}', '"blue"')
WHERE id = 7;
Функция jsonb_array_elements
разворачивает JSON-массив в набор строк:
SELECT id, jsonb_array_elements(preferences->'items') AS item
FROM users;
Функция jsonb_concat
объединяет два JSON-объекта:
SELECT jsonb_concat(preferences, '{"new_key": "new_value"}') AS new_preferences
FROM users;