Today I Learned/SQL

postgresql json data로 변환하고 테이블로 만들어서 사용하기

하나719 2023. 7. 13. 10:41
반응형

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 This section describes: functions and …

www.postgresql.org

1. json type의 테이블 만들기

post_json -> table name

아래처럼 테이블을 생성하고 보면, type = jsonb 로 출력된다. 

CREATE TABLE post_json (jsondata jsonb);

2. 원하는 데이터를 json 형태로 위 테이블에 insert

row_to_json( subquery 명 )  

 

insert into post_json(jsondata) 
select row_to_json(Q) as json_data 
from (select * from books) Q;

json type으로 생성

3. jsonb_pretty(컬럼명) 

이 함수로 보기 쉽게 보여줄 수 있다.

SELECT jsonb_pretty(jsondata) FROM post_json;

4. Where절에 조건 걸기: @>

SELECT jsonb_pretty(jsondata) FROM post_json WHERE jsondata @> '{"author":"김승호"}';

 

반응형