JSON 代表 JavaScript Object
Notation。JSON是开放的标准格式,由key-value对组成。JSON的主要用于在服务器与web应用之间传输数据。
PostgreSQL提供了两种存储JSON数据的类型:json和jsonb;
jsonb是json的二进制形式。
json格式写入快,但读取慢;
jsonb格式写入慢,但读取快;
常用语法
// -> 返回jsonselect '[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json->2 // 输出 {"c":"baz"}select '{"a": {"b":"foo"}, "c":{"a": "aaa"}}'::json->'a' // 输出 {"b":"foo"}// ->> 返回文本select '[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json->>2 // 输出 {"c":"baz"}select '{"a": {"b":"foo"}, "c":{"a": "aaa"}}'::json->>'a' // 输出 {"b":"foo"}// #> 获取json子对象select '{"a": {"b":{"c": "foo"}}}'::json#> '{a,b}' // 输出 {"c": "foo"}select '{"a": {"b":{"c": "foo"}}}'::json#>> '{a,b}' // 输出 {"c": "foo"}// @> ———— 判断第一个json是否包含第二个select '{"a":1, "b":2}'::jsonb @> '{"b":2}'::jsonb //输出t// <@ ———— 判断第一个json是否在第一个中select '{"b":2}'::jsonb <@ '{"a":1, "b":2}'::jsonb //输出t
常用操作符
实例操作
数据库表(test_pgsql):
第一条data为json类型的数组:
[{"username": "张三","age": "18","sex": "男"},{"username": "张三","age": "25","sex": "男"},{"username": "马冬梅","age": "20","sex": "女"}
]
第二条data内容为json类型的对象:
{"course":[{"id":"1","course":"语文","score":"75"},{"id":"2","course":"数学","score":"100"}],"student":{"name":"小王","age":"22"}
}
1、查询数组的长度:
// 结果为4
select jsonb_array_length(t."data"::jsonb) from test_pgsql t WHERE t.id = 1;
2、查询数组中第二个元素
//数组索引从0开始,结果为{"age": "25", "sex": "男", "username": "张三"}
select t."data"::jsonb->>1 from test_pgsql t WHERE t.id = 1;
3、查询data中所有的username
select json_array_elements(t."data"::json) #> '{username}' as guid from test_pgsql t where t."id" = 1;//username去重
select distinct guid|| '' from (select json_array_elements(t."data"::json) #> '{username}' as guid from test_pgsql t where t."id" = 1) tmp;
结果:
4、json_object_keys 用法
说明:json_object_keys 不能用于纯数组
//获取json中的键
select json_object_keys(t."data"::json) from test_pgsql t where id = 2;
5、json_array_elements 用法
//json_array_elements用于提取转换纯数组元素,将数组拆分为单独记录
select json_array_elements(t."data"::json) from test_pgsql t WHERE t.id = 1;
6、json_extract_path 用法
说明:json_extract_path不能直接操作纯数组
//查询json中指定键(student)的值,结果为 {"name": "小王", "age": "22"}
select json_extract_path(t."data"::json, 'student') from test_pgsql t where id = 2;
//结果为 "小王"
select json_extract_path(t."data"::json, 'student','name') from test_pgsql t where id = 2;//和 #>操作符 是一样的
select t."data"::json #>'{student}' from test_pgsql t where id = 2;
select t."data"::json #>'{student,name}' from test_pgsql t where id = 2;
示例:
新建表如下:
CREATE TABLE "public"."biz_orders" ( "ID" int8 NOT NULL DEFAULT nextval('"biz_orders_ID_seq"'::regclass),"info" json NOT NULL
);
表初始化语句:
INSERT INTO "biz_orders"("ID", "info") VALUES (1, '{"name":"张三","items":{"product":"啤酒","qty":6}}');
INSERT INTO "biz_orders"("ID", "info") VALUES (2, '{"name":"李四","items":{"product":"辣条","qty":8}}');
INSERT INTO "biz_orders"("ID", "info") VALUES (3, '{"name":"王五","items":{"product":"苹果","qty":18}}');
INSERT INTO "biz_orders"("ID", "info") VALUES (4, '{"name":"赵一","items":{"product":"香蕉","qty":20}}');
使用
1、简单查询
select * from biz_orders;
2、查询使用->操作符,查询json中所有顾客作为键
SELECT info -> 'name' AS customer FROM biz_orders;
3、下面使用->>操作获取所有顾客姓名作为值
SELECT info ->> 'name' AS customer FROM biz_orders;
4、根据json对象的key查询值
SELECTinfo -> 'items' ->> 'product' as product
FROMbiz_orders
ORDER BYproduct;
5、where查询中使用json字段
SELECTinfo ->> 'name' AS customer
FROMbiz_orders
WHEREinfo -> 'items' ->> 'product' = '辣条'
6、case 查询
SELECTinfo ->> 'name' AS customer,info -> 'items' ->> 'product' AS product
FROMbiz_orders
WHERECAST (info -> 'items' ->> 'qty' AS INTEGER) = 6
7、聚合函数
SELECTMIN( CAST ( info -> 'items' ->> 'qty' AS INTEGER ) ),MAX( CAST ( info -> 'items' ->> 'qty' AS INTEGER ) ),SUM( CAST ( info -> 'items' ->> 'qty' AS INTEGER ) ),AVG( CAST ( info -> 'items' ->> 'qty' AS INTEGER ) )
FROMbiz_orders;
8、类型查询
SELECTjson_typeof ( info -> 'items' -> 'qty' )
FROMbiz_orders;