-- json和jsonb共有的運算符 -- 獲取JSON數組元素(索引從0開始,從末尾開始計算負整數) select '[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json->2 -- {"c":"baz"} select '[{"a":"foo"},{"b":"bar"},{"c":"baz"}]'::json->-1 -- {"c":"baz"} -- 按KEY獲取JSON對象字段 select '{"a": {"b":"foo"}}'::json->'a' -- {"b":"foo"} -- 和->唯一的區別就是獲取的值是文本 select '[1,2,3]'::json->>2 -- 3 select '{"a":1,"b":2}'::json->>'b' -- 2 -- 可以深度的獲取JSON對象 select '{"a": {"b":{"c": "foo"}}}'::json#>'{a,b}' -- {"c": "foo"} -- 可以深度的獲取JSON對象作為文本 select '{"a":[1,2,3],"b":[4,5,6]}'::json#>>'{a,2}' -- 3 -- jsonb特有的運算符 -- 左側JSON值是否包含頂級正確的JSON路徑,通俗點就是我們數學上說的包含關系 select '{"a":1, "b":2}'::jsonb @> '{"b":2}'::jsonb -- true select '{"a":1, "b":2}'::jsonb @> '{"b":4}'::jsonb -- false select '{"a":1, "b":2}'::jsonb @> '{"c":4}'::jsonb -- false select '{"b":2}'::jsonb <@ '{"a":1, "b":2}'::jsonb -- true select '{"c":2}'::jsonb <@ '{"a":1, "b":2}'::jsonb -- false select '["a","b","c"]'::jsonb @> '["a"]' ::jsonb -- true -- 這個字符串是否存在頂級鍵 select '{"a":1, "b":2}'::jsonb ? 'b' -- true -- 這個字符串數組是否有一個是頂級鍵 select '{"a":1, "b":2, "c":3}'::jsonb ?| array['b', 'c'] -- true select '{"a":1, "b":2, "c":3}'::jsonb ?| array['b', 'd'] -- false -- 這個字符串數組是否都是頂級鍵 select '["a", "b"]'::jsonb ?& array['a', 'b'] -- true select '["a", "b"]'::jsonb ?& array['a', 'c'] -- false -- 將兩個jsonb值連接成一個新的jsonb值 select '["a", "b"]'::jsonb || '["c", "d"]'::jsonb -- 時間過長 1.922s -- 刪除鍵值對 select '{"a":1, "b":2}'::jsonb - 'a' select '{"a":1, "b":2}'::jsonb - 'b' -- 刪除具有指定索引的數組元素(從末尾開始計算負整數)。如果頂級容器不是數組,則會引發錯誤。 select '["a", "b"]'::jsonb - 1 -- 刪除具有指定路徑的字段或元素(對於JSON數組,從末尾開始計算負整數) select '["a", {"b":1}]'::jsonb #- '{1,b}' -- json創建函數 -- 返回值為json或jsonb select to_json('Fred said "Hi."'::text) -- "Fred said \"Hi.\"" select to_jsonb('Fred said "Hi."'::text) -- "Fred said \"Hi.\"" -- 將數組作為JSON數組返回 select array_to_json('{{1,5},{99,100}}'::int[]) -- [[1,5],[99,100]] select array_to_json('{{1,5},{99,100}}'::int[],true) -- [[1,5],[99,100]] 中間會有個換行符 -- 將行作為JSON對象返回,這個應該比較少用吧 select row_to_json(row(1,'foo')) -- {"f1":1,"f2":"foo"} -- 從可變參數列表構建可能異構類型的JSON數組。 select json_build_array(1,2,'3',4,5) select jsonb_build_array(1,2,'3',4,5) -- [1, 2, "3", 4, 5] -- 從可變參數列表構建JSON對象。按照慣例,參數列表由交替的鍵和值組成。 select json_build_object('foo',1,'bar',2) select jsonb_build_object('foo',1,'bar',2) -- {"bar": 2, "foo": 1} -- 以下三種格式都可以轉換成json對象 -- 從文本數組中構建JSON對象。數組必須具有一個具有偶數個成員的維度,在這種情況下,它們被視為交替的鍵/值對,或者兩個維度,使得每個內部數組具有恰好兩個元素,這些元素被視為鍵/值對。 select json_object('{a, 1, b, "def", c, 3.5}') -- {"a" : "1", "b" : "def", "c" : "3.5"} select json_object('{{a, 1},{b, "def"},{c, 3.5}}') -- {"a" : "1", "b" : "def", "c" : "3.5"} select json_object('{a, b}', '{1,2}') -- {"a" : "1", "b" : "2"} -- JSON處理函數 -- 獲取當前最外層json數組的長度 select json_array_length('[1,2,3,{"f1":1,"f2":[5,6]},4]') -- 5 -- 將最外層的JSON對象擴展為一組鍵/值對。 select * from json_each('{"a":"foo", "b":"bar"}') select * from json_each('{"a":"foo", "b":{"f1":1,"f2":[5,6]}}') -- 和上面一樣,只不過是text類型。 select * from json_each_text('{"a":"foo", "b":"bar"}') -- 返回path_elems指向的JSON值(相當於#>運算符)。 select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4') -- {"f5":99,"f6":"foo"} select json_extract_path('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4','f5') -- 99 -- 返回path_elems指向的JSON值作為文本(相當於#>> 運算符)。 select json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4', 'f6') --foo -- 返回最外層JSON對象中的鍵集。 select json_object_keys('{"f1":"abc","f2":{"f3":"a", "f4":"b"}}') -- 以下兩個官方例子無法使用 select * from json_populate_record(null::myrowtype, '{"a":1,"b":2}') select * from json_populate_recordset(null::myrowtype, '[{"a":1,"b":2},{"a":3,"b":4}]') -- 將JSON數組擴展為一組JSON值。 select * from json_array_elements('[1,true, [2,false]]') select * from json_array_elements_text('["foo", "bar"]') -- 以文本字符串形式返回最外層JSON值的類型 select json_typeof('-123.4') --number select json_typeof('[1,true, [2,false]]') --array select json_typeof('true') --boolean -- 這些有點偏 select * from json_to_record('{"a":1,"b":[1,2,3],"c":"bar"}') as x(a int, b text, d text) select * from json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","c":"bar"}]') as x(a int, b text); select json_strip_nulls('[{"f1":1,"f2":null},2,null,3]') -- 這個專門替換的,昨天就是用這個,效果那叫一個好,默認最后是true,無則添加 select jsonb_set('[{"f1":1,"f2":null},2,null,3]', '{0,f1}','[2,3,4]', false) select jsonb_set('[{"f1":1,"f2":null},2]', '{0,f3}','[2,3,4]') -- 縮進的JSON文本返回 select jsonb_pretty('[{"f1":1,"f2":null},2,null,3]')