#17 PostgreSQL JSON Columns
#17.0 Introduction
JSON coulmn data type ์ผ๋ก ๋ค์ํ ์์ ๊ฐ๋ฅ
#17.1 JSON and JSONB
CREATE TABLE users (
user_id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
profile JSONB
);
-- JSON ์
๋ ฅ๋ TEXT๋ฅผ ๋ณต์ฌํด ์ ์ฅ
-- JSONB ๋ถํด๋ Binary ํ์์ผ๋ก ์ ์ฅ > ์ฒ๋ฆฌ ์๋ ๋น ๋ฆ
INSERT INTO users (profile) VALUES
('{"name": "Taco", "age": 30, "city": "Budapest"}'),
-- SELECT json_build_object('name', 'Taco', 'age', 30, 'city', 'Budapest')
('{"name": "Giga", "age": 25, "city": "Tbilisi", "hobbies": ["reading", "climbing"]}')
-- SELECT json_build_object('name', 'Giga', 'age', 25, 'city', 'Tbilisi', 'hobbies', json_build_array('reading', 'climbing'))
;
#17.2 Querying JSON
SELECT
profile -> 'name' AS name,
profile -> 'city' AS city,
profile -> 'age' AS age,
profile -> 'hobbies' -> 0 AS first_hobby
FROM
users;
-- ์กฐ๊ฑด
SELECT
profile -> 'name' AS name,
profile -> 'city' AS city,
profile -> 'age' AS age,
profile -> 'hobbies' -> 0 AS first_hobby
FROM users
WHERE profile ? 'hobbies';
SELECT
profile -> 'name' AS name,
profile -> 'city' AS city,
profile -> 'age' AS age,
profile -> 'hobbies' -> 0 AS first_hobby
FROM
users
WHERE profile -> 'hobbies' ? 'reading';
-- JSONB
SELECT
profile -> 'name' AS name,
profile -> 'city' AS city,
jsonb_array_length(profile -> 'hobbies') AS first_hobby
FROM users;
SELECT
profile -> 'name' AS name,
profile -> 'city' AS city,
jsonb_array_length(profile -> 'hobbies') AS first_hobby
FROM users
WHERE profile ->> 'name' = 'Taco';
SELECT
profile -> 'name' AS name,
profile -> 'city' AS city,
jsonb_array_length(profile -> 'hobbies') AS first_hobby
FROM users
WHERE (profile ->> 'age')::integer < 30;
SELECT
profile -> 'name' AS name,
profile -> 'city' AS city,
jsonb_array_length(profile -> 'hobbies') AS first_hobby
FROM users
WHERE profile ?& array['name', 'hobbies'];
WHERE profile -> 'hobbies' ?| array['reading', 'traveling'];
WHERE profile -> 'hobbies' ?& array['reading', 'traveling'];
SELECT
profile -> 'name' AS name,
profile -> 'city' AS city,
jsonb_array_length(profile -> 'hobbies') AS first_hobby
FROM users
WHERE profile ->> 'city' LIKE 'B%';
https://www.postgresql.org/docs/9.5/functions-json.html
#17.3 Processing JSON
-- update
UPDATE users
SET profile = profile || jsonb_build_object('email', 'x@x.com')::jsonb;
UPDATE users
SET profile = profile - 'email'
WHERE profile ->> 'name' = 'Giga';
UPDATE users
SET profile = profile || jsonb_build_object('hobbies', jsonb_build_array('climbing', 'traveling'))
WHERE profile ->> 'name' = 'Taco';
-- select ์์ ์ฌ์ฉ๊ฐ๋ฅ
SELECT (profile -> 'hobbies') - 'climbing'
FROM users;
UPDATE users
SET profile = profile || jsonb_set(profile, '{hobbies}', (profile -> 'hobbies') - 'climbing');
Last updated
Was this helpful?