Postgresql - Functions and Operators 函数和运算 - JSON

json数据相关的函数和计算。

越来越的人喜欢直接把json数据存入数据库,各个数据库也都在支持json类型,所以对于json相关的函数和运算应该掌握了解。

Operator

Right Operand Type

Description

Example

Example Result

->

int

获取json中的元素

'[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json->2

{"c":"baz"}

->

text

获取json中的对象

'{"a": {"b":"foo"}}'::json->'a'

{"b":"foo"}

->>

int

获取json数组中的元素

'[1,2,3]'::json->>2

3

->>

text

获取json中的对象

'{"a":1,"b":2}'::json->>'b'

2

#>

text[]

从给定的路径中查询json对象

'{"a": {"b":{"c": "foo"}}}'::json#>'{a,b}'

{"c": "foo"}

#>>

text[]

从给定的路径中查询json对象

'{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}'

3

Operator

Right Operand Type

Description

Example

@>

jsonb

判断是否包含

'{"a":1, "b":2}'::jsonb @> '{"b":2}'::jsonb

<@

jsonb

判断是否包含

'{"b":2}'::jsonb <@ '{"a":1, "b":2}'::jsonb

?

text

判断是否包含

'{"a":1, "b":2}'::jsonb ? 'b'

?|

text[]

判断是否包含其中一个

'{"a":1, "b":2, "c":3}'::jsonb ?| array['b', 'c']

?&

text[]

判断是否包含全部

'["a", "b"]'::jsonb ?& array['a', 'b']

||

jsonb

连接

'["a", "b"]'::jsonb || '["c", "d"]'::jsonb

-

text

删除键值对

'{"a": "b"}'::jsonb - 'a'

-

text[]

删除键值对

'{"a": "b", "c": "d"}'::jsonb - '{a,c}'::text[]

-

integer

删除数组中的元素

'["a", "b"]'::jsonb - 1

#-

text[]

删除json数组中的元素

'["a", {"b":1, "c":2}]'::jsonb #- '{1,b}'

Function

Description

Example

Example Result

to_json(anyelement)

to_jsonb(anyelement)

转换为JSON或JSONB。数组和复合类型被(递归地)转换为数组和对象;否则,如果存在从类型到JSON的强制转换,则将使用计算函数执行转换;否则,将产生标量值。对于任何除数字、布尔值或空值以外的任何标量类型,文本表示将以这样的方式使用,即它是有效的JSON或JSONB值。

to_json('Fred said "Hi."'::text)

"Fred said \"Hi.\""

array_to_json(anyarray [, pretty_bool])

将数组作为JSON数组返回。PostgreSQL多维数组成为数组的JSON数组。

array_to_json('{{1,5},{99,100}}'::int[])

[[1,5],[99,100]]

row_to_json(record [, pretty_bool])

将行作为JSON对象返回。如果pretty_bool是true,将在 level-1 元素之间添加行反馈。

row_to_json(row(1,'foo'))

{"f1":1,"f2":"foo"}

json_build_array(VARIADIC "any")

jsonb_build_array(VARIADIC "any")

从变量参数列表中构建可能不均匀类型的JSON数组。

json_build_array(1,2,'3',4,5)

[1, 2, "3", 4, 5]

json_build_object(VARIADIC "any")

jsonb_build_object(VARIADIC "any")

从变量参数列表中构建JSON对象。按照惯例,参数列表由交替键和值组成。

json_build_object('foo',1,'bar',2)

{"foo": 1, "bar": 2}

json_object(text[])

jsonb_object(text[])

从文本数组中构建JSON对象。该数组必须具有精确的一维且具有偶数个成员,在这种情况下,它们被用作交替的键/值对,或者两个维度,使得每个内部数组具有正好两个元素,它们被用作键/值对。

json_object('{a, 1, b, "def", c, 3.5}')

json_object('{{a, 1},{b, "def"},{c, 3.5}}')

{"a": "1", "b": "def", "c": "3.5"}

json_object(keys text[], values text[])

jsonb_object(keys text[], values text[])

json_object形式从两个单独的数组中获得键和值

json_object('{a, b}', '{1,2}')

{"a": "1", "b": "2"}

Function

Return Type

Description

Example

Example Result

json_array_length(json)

jsonb_array_length(jsonb)

int

JSON数组中元素的个数

json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]')

5

json_each(json)

jsonb_each(jsonb)

setof key text, value json

setof key text, value jsonb

将JSON数据设置为键值对输出

select * from json_each('{"a":"foo", "b":"bar"}')

key | value

-----+-------

a | "foo"

b | "bar"

json_each_text(json)

jsonb_each_text(jsonb)

setof key text, value text

将JSON数据设置为键值对输出。返回的值设为text类型

select * from json_each_text('{"a":"foo", "b":"bar"}')

key | value

-----+-------

a | foo

b | bar

json_extract_path(from_json json, VARIADIC path_elems text[])

jsonb_extract_path(from_json jsonb, VARIADIC path_elems text[])

json

jsonb

根据路径,返回json值

json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4')

{"f5":99,"f6":"foo"}

json_extract_path_text(from_json json, VARIADIC path_elems text[])

jsonb_extract_path_text(from_json jsonb, VARIADIC path_elems text[])

text

根据路径返回json值

json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4', 'f6')

foo

json_object_keys(json)

jsonb_object_keys(jsonb)

setof text

返回json中的所有键(key)

json_object_keys('{"f1":"abc","f2":{"f3":"a", "f4":"b"}}')

json_object_keys

------------------

f1

f2

json_populate_record(base anyelement, from_json json)

jsonb_populate_record(base anyelement, from_json jsonb)

anyelement

将from_json中的对象展开为其列与BASE定义的记录类型匹配的行。

select * from json_populate_record(null::myrowtype, '{"a": 1, "b": ["2", "a b"], "c": {"d": 4, "e": "a b c"}}')

a | b | c

---+-----------+-------------

1 | {2,"a b"} | (4,"a b c")

json_populate_recordset(base anyelement, from_json json)

jsonb_populate_recordset(base anyelement, from_json jsonb)

setof anyelement

将from_json中最外的对象数组扩展为一组行,其行与基定义的记录类型相匹配

select * from json_populate_recordset(null::myrowtype, '[{"a":1,"b":2},{"a":3,"b":4}]')

a | b

---+---

1 | 2

3 | 4

json_array_elements(json)

jsonb_array_elements(jsonb)

setof json

setof jsonb

返回json数组

select * from json_array_elements('[1,true, [2,false]]')

value

-----------

1

true

[2,false]

json_array_elements_text(json)

jsonb_array_elements_text(jsonb)

setof text

返回json的属组value

select * from json_array_elements_text('["foo", "bar"]')

value

-----------

foo

bar

json_typeof(json)

jsonb_typeof(jsonb)

text

将最外JSON值的类型作为文本字符串返回。可能的类型是对象、数组、字符串、数字、布尔和null。

json_typeof('-123.4')

number

json_to_record(json)

jsonb_to_record(jsonb)

record

从对象的JSON数组构建任意一组记录。与所有函数返回记录一样,调用必须用as子句显式定义记录的结构

select * from json_to_record('{"a":1,"b":[1,2,3],"c":[1,2,3],"e":"bar","r": {"a": 123, "b": "a b c"}}') as x(a int, b text, c int[], d text, r myrowtype)

a | b | c | d | r

---+---------+---------+---+---------------

1 | [1,2,3] | {1,2,3} | | (123,"a b c")

json_to_recordset(json)

jsonb_to_recordset(jsonb)

setof record

从对象的JSON数组构建任意一组记录。与所有函数返回记录一样,调用必须用as子句显式定义记录的结构

select * from json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","c":"bar"}]') as x(a int, b text);

a | b

---+-----

1 | foo

2 |

json_strip_nulls(from_json json)

jsonb_strip_nulls(from_json jsonb)

json

jsonb

返回from_json中所有空值的对象字段。其他空值未被触碰。

json_strip_nulls('[{"f1":1,"f2":null},2,null,3]')

[{"f1":1},2,null,3]

jsonb_set(target jsonb, path text[], new_value jsonb[,create_missing boolean])

jsonb

返回由new_value替换的路径指定的目标,或者如果create_missing设置为true并且路径指定的项不存在,new_value被添加。

jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0,f1}','[2,3,4]', false)

jsonb_set('[{"f1":1,"f2":null},2]', '{0,f3}','[2,3,4]')

[{"f1":[2,3,4],"f2":null},2,null,3]

[{"f1": 1, "f2": null, "f3": [2, 3, 4]}, 2]

jsonb_insert(target jsonb, path text[], new_value jsonb, [insert_after boolean])

jsonb

返回被插入的new_value。如果由路径指定的目标段位于JSONB数组中,则new_value将在目标之前插入,或之后插入为true(默认之后插入)。如果由路径指定的目标段在JSONB对象中,则仅当目标不存在时才插入new_value。与路径为导向的运算符一样,从JSON数组的末尾出现在路径计数中的负整数。

jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"')

jsonb_insert('{"a": [0,1,2]}', '{a, 1}', '"new_value"', true)

{"a": [0, "new_value", 1, 2]}

{"a": [0, 1, "new_value", 2]}

jsonb_pretty(from_json jsonb)

text

将JSON数据格式化为标准格式

jsonb_pretty('[{"f1":1,"f2":null},2,null,3]')

[

{

"f1": 1,

"f2": null

},

2,

null,

3

]

猜你喜欢

转载自blog.csdn.net/chuckchen1222/article/details/81454544