
โ JSON Column์ด๋?
ํ ์ด๋ธ ์์ JSON ํ์ ๋ฐ์ดํฐ๋ฅผ ๊ทธ๋๋ก ์ ์ฅํ๋ Column
MySQL๊ณผ PostgreSQL ๋ชจ๋ JSON ๋ฐ์ดํฐ ํ์ ์ผ๋ก ์ ์ฅํ๋ ๊ฑธ ํ์ฉํ๋ค.
SQL์ด๋ฉด์ ๋์์ NoSQL์ฒ๋ผ ๋์ํ๋ค.
์ฌ์ฉ ์๊ธฐ: ์ ์ฐํ ๊ตฌ์กฐ๊ฐ ํ์ํ ๋(์ด๋ ํ column์ ์ด๋ค ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ์ผ ํ ์ง ๋ชจ๋ฅผ ๋)
์ ์ฐ์ฑ↑, ๊ตฌ์กฐ ์์ ์ฑ↓
โ JSON, JSONB
๊ฑฐ์ ๋์ผํ ๊ฐ์ ๊ฐ์ง๋ค.
์ฐจ์ด์ ์ ํจ์จ์ฑ์ด๋ค.
JSON vs JSONB
JSON: ์ ๋ ฅ๋ text๋ฅผ ๊ทธ๋๋ก ๋ณต์ฌํด์ ์ ์ฅํจ
JSONB: ๋ถํด๋ Binary ํ์์ผ๋ก ์ ์ฅํจ(๋ฐ์ดํฐ ์ ๋ ฅ ์๋๊ฐ ์ฝ๊ฐ ๋๋ ค์ง๋ค. JSON์์ Binary๋ก ๋ณํํด์ผ ํ๊ธฐ ๋๋ฌธ, ์ฒ๋ฆฌ ์๋๋ ์์ฒญ ๋นจ๋ผ์ง, ๋๋ถ๋ถ JSONB ์ ํธ)
CREATE TABLE users (
user_id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
profile JSONB
);
INSERT INTO
users (profile)
VALUES
(json_build_object('name', 'Taco', 'age', 30, 'city', 'Budapest')),
(json_build_object('name', 'Giga', 'age', 25, 'city', 'Tbilisi', 'hobbies', json_build_array('reading', 'climbing')));
json_build_object, json_build_array
์ธ์์ ์๋ง์ ๊ฐ์ ์ฃผ๋ฉด ๊ฐ๊ฐ object์ array๋ฅผ ๋ง๋ค์ด์ค๋ค.
https://www.postgresql.org/docs/current/datatype-json.html
8.14. JSON Types
8.14. JSON Types # 8.14.1. JSON Input and Output Syntax 8.14.2. Designing JSON Documents 8.14.3. jsonb Containment and Existence 8.14.4. jsonb …
www.postgresql.org
โ Querying JSON
SELECT
profile -> 'name',
profile -> 'age',
profile -> 'city'
FROM
users;

SELECT
profile ->> 'name',
profile ->> 'age',
profile ->> 'city'
FROM
users;
JSON ํ์์ผ๋ก ์ ์ฅ๋์ด์ ๋ถ์ " " ์์ ๊ธฐ

SELECT
profile ->> 'name' AS name,
profile ->> 'age' AS age,
profile ->> 'city' AS city,
profile -> 'hobbies' ->> 0 AS first_hobby
FROM
users;
JSON ๋ด๋ถ์ ๋ฐฐ์ด ์ด๊ธฐ, Column ์ด๋ฆ ์ ํด์ฃผ๊ธฐ

Filtering Data on JSON Column
SELECT
profile ->> 'name' AS name,
profile ->> 'age' AS age,
profile ->> 'city' AS city,
profile -> 'hobbies' ->> 0 AS first_hobby
FROM
users
WHERE
profile ? 'hobbies';
profile์ hobbies key๊ฐ ์๋ ๋ฐ์ดํฐ๋ง ๊ฐ์ ธ์ด
SELECT
profile ->> 'name' AS name,
profile ->> 'age' AS age,
profile ->> 'city' AS city,
profile -> 'hobbies' ->> 0 AS first_hobby
FROM
users
WHERE
profile -> 'hobbies' ? 'climbing';
hobbies์ climbing์ ๊ฐ์ง๊ณ ์๋ ๋ฐ์ดํฐ๋ง ๊ฐ์ ธ์ด
SELECT
profile ->> 'name' AS name,
profile ->> 'age' AS age,
jsonb_array_length(profile -> 'hobbies') AS total_hobbies,
profile -> 'hobbies' ->> 0 AS first_hobby
FROM
users
WHERE
(profile ->> 'age')::INTEGER < 30;
profile์์ age๋ฅผ ๊ฐ์ ธ์์ ๋ text์ด๊ธฐ ๋๋ฌธ์ integer๋ก ๋ณํ์์ผ์ค์ผ ํ๋ค.
๊ทธ๋ฆฌ๊ณ jsonb๋ฅผ ์ฌ์ฉํ๊ณ ์์ผ๋ฉด jsonb ํจ์๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
?|
SELECT
profile ->> 'name' AS name,
profile ->> 'age' AS age,
jsonb_array_length(profile -> 'hobbies') AS total_hobbies,
profile -> 'hobbies' ->> 0 AS first_hobby
FROM
users
WHERE
profile -> 'hobbies' ?| ARRAY['reading', 'traveling'];
?| ์ฐ์ฐ์๋ ๋ค์ ์ฃผ์ด์ง๋ ์กฐ๊ฑด์ด ์ด๋ ๊ฒ ํ๋๋ผ๋ ํฌํจ๋๋ฉด ๋ง์กฑ๋๋ or ์ฐ์ฐ์์ด๋ค.
?&
SELECT
profile ->> 'name' AS name,
profile ->> 'age' AS age,
jsonb_array_length(profile -> 'hobbies') AS total_hobbies,
profile -> 'hobbies' ->> 0 AS first_hobby
FROM
users
WHERE
profile -> 'hobbies' ?& ARRAY['reading', 'traveling'];
and ์ฐ์ฐ์
SELECT
profile ->> 'name' AS name,
profile ->> 'age' AS age,
jsonb_array_length(profile -> 'hobbies') AS total_hobbies,
profile -> 'hobbies' ->> 0 AS first_hobby,
profile ->> 'city' AS city
FROM
users
WHERE
profile ->> 'city' LIKE 'B%';
LIKE ๊ฒฐํฉํด์ ์ฌ์ฉ
โ Processing JSON
||
UPDATE users
SET
profile = profile || jsonb_build_object('email', 'x@x.com');
json๊ณผ json์ ๋ฐ์์ ํฉ์ณ์ง json์ ๋ฐํ
-
UPDATE users
SET
profile = profile - 'email'
WHERE
profile ->> 'name' = 'Giga';
key์ value ์ ๊ฑฐ
jsonb_set
UPDATE users
SET
profile = profile || jsonb_set(profile, '{hobbies}', (profile -> 'hobbies') || jsonb_build_array('cooking'));
์ธ์๋ก target, path, new_value๋ฅผ ๋ฃ์ด์ฃผ๋ฉด target์ ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ค.(์ค์ฒฉ๋ ๋ฐ์ดํฐ์ ๊น์ด ๋ค์ด๊ฐ ์์ ํ๊ณ ์ถ์ ๋ ์ฌ์ฉ)
๋ง์ง๋ง ์ธ์๋ก๋ optional์ธ path์ ํด๋นํ๋ ๊ฐ์ด ๋ถ์ฌ์ผ ๋ ์๋ก ์์ฑํ๋ ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ boolean ๊ฐ์ด ์๋ค.
(true์ด๋ฉด ์ path๋ก ์์ฑ, false๋ฉด ํด๋นํ๋ path๊ฐ ์์ ๋ ๊ทธ๋ฅ ๋ฌด์)
https://www.postgresql.org/docs/current/functions-json.html
9.16. JSON Functions and Operators
9.16. JSON Functions and Operators # 9.16.1. Processing and Creating JSON Data 9.16.2. The SQL/JSON Path Language 9.16.3. SQL/JSON Query Functions …
www.postgresql.org
'๐งฉ SQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| SQL - SQL Injection (0) | 2026.04.09 |
|---|---|
| SQL - PostgreSQL, Extensions (0) | 2026.04.06 |
| SQL - PostgreSQL, DCL(Data Control Language) (0) | 2026.04.03 |
| SQL - PostgreSQL, Transaction, Save Point, Isolation Level, Phenomena (0) | 2026.04.02 |
| SQL - PostgreSQL, Extension(plpython3) (0) | 2026.04.02 |