2.1、json应用

2.1.1、创建json类型字段表

  1. postgres=# create table t_json(id int,f_json json);
  2. NOTICE: Replica identity is needed for shard table, please add to this table through "alter table" command.
  3. CREATE TABLE

2.1.2、插入数据

  1. postgres=# insert into t_json values(1,'{"col1":1,"col2":"tbase"}');
  2. INSERT 0 1
  3. postgres=# insert into t_json values(2,'{"col1":1,"col2":"tbase","col3":"pgxz"}');
  4. INSERT 0 1
  5. postgres=# select * from t_json;
  6. id | f_json
  7. ----+-----------------------------------------
  8. 1 | {"col1":1,"col2":"tbase"}
  9. 2 | {"col1":1,"col2":"tbase","col3":"pgxz"}
  10. (2 rows)

2.1.3、通过键获得 JSON 对象域

  1. postgres=# select f_json ->'col2' as col2 ,f_json -> 'col3' as col3 from t_json;
  2. col2 | col3
  3. ---------+--------
  4. "tbase" |
  5. "tbase" | "pgxz"
  6. (2 rows)

2.1.4、以文本形式获取对象值

  1. postgres=# select f_json ->>'col2' as col2 ,f_json ->> 'col3' as col3 from t_json;
  2. col2 | col3
  3. -------+------
  4. tbase |
  5. tbase | pgxz
  6. (2 rows)
  7. postgres=# select f_json ->>'col2' as col2 ,f_json ->> 'col3' as col3 from t_json where f_json ->> 'col3' is not null;
  8. col2 | col3
  9. -------+------
  10. tbase | pgxz
  11. (1 row)