関数 | 戻り値 | 説明 | 例 | 結果例 |
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)
| set of key text, value json
set of 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)
| set of 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
| path_elems で指定されたパスにある 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
| path_elems 引数で指定されたパスにある JSON 値を text として返します。この操作は #>> オペレーターと同等です。
| json_extract_path_text('{"f2":{"f3":1},"f4":{"f5":99,"f6":"foo"}}','f4', 'f6')
| foo
|
json_object_keys(json)
jsonb_object_keys(jsonb)
| set of text
| 最も外側の JSON オブジェクト内のキーのセットを返します。 | json_object_keys('{"f1":"abc","f2":{"f3":"a", "f4":"b"}}')
| json_object_keys
------------------
f1
f2
|
json_populate_record(base any element, from_json json)
jsonb_populate_record(base any element, from_json jsonb)
| any element
| 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 any element, from_json json)
jsonb_populate_recordset(base any element, from_json jsonb)
| set of any element
| from_json 引数で指定されたオブジェクトの最も外側の配列を、base 引数で指定されたレコードタイプに一致する列を持つ行のセットに展開します。
| 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)
| set of json
set of jsonb
| JSON 配列を 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)
| set of text
| json 配列を text 値のコレクションに展開します。
| SELECT * FROM json_array_elements_text('["foo", "bar"]')
| value
-----------
foo
bar
|
json_typeof(json)
jsonb_typeof(jsonb)
| text
| 最も外側の json 値の型をテキスト文字列として返します。可能な型は、object、array、string、number、boolean、および null です。 | json_typeof('-123.4')
| number
|
json_to_record(json)
jsonb_to_record(jsonb)
| record
| json オブジェクトから任意のレコードを構築します。record を返す全ての関数と同様に、AS 句を使用してレコードの構造を明示的に定義する必要があります。
| SELECT * FROM json_to_record('{"a":1,"b":[1,2,3],"c":"bar"}')
as x(a int, b text, c text)
| a | b | c |
---+---------+-----+
1 | [1,2,3] | bar |
|
json_to_recordset(json)
jsonb_to_recordset(jsonb)
| set of record
| オブジェクトの JSON 配列から任意のレコードのセットを構築します。record を返す全ての関数と同様に、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 引数の入力値を返しますが、null 値を持つすべてのオブジェクトフィールドは省略され、他の null 値は保持されます。
| json_strip_nulls('[{"f1":1,"f2":null},2,null,3]')
| [{"f1":1},2,null,3]
|
jsonb_set(jsonb, path text[], new_value jsonb[,create_missing boolean])
| jsonb
| 指定された path にあるアイテムを new_value に置き換えた jsonb を返します。指定された path にアイテムが存在せず、create_missing が true (デフォルトは true) の場合、new_value が追加されます。パス指向のオペレーターと同様に、path に現れる負の整数は、json 配列の末尾からカウントされます。 | 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 を返します。path で指定されたターゲットが jsonb 配列内にある場合、new_value はターゲットの前 (insert_after がデフォルトの false の場合) またはターゲットの後 (insert_after が true の場合) に挿入されます。path で指定されたターゲットが jsonb オブジェクト内にある場合、new_value はターゲットキーがまだ存在しない場合にのみ挿入されます。パス指向のオペレーターの場合、path に現れる負の整数は 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
| from_json 引数の入力値を、インデントされた JSON テキストとして返します。
| jsonb_pretty('[{"f1":1,"f2":null},2,null,3]')
| [
{
"f1": 1,
"f2": null
},
2,
null,
3
]
|
jsonb_path_exists(target jsonb, path jsonpath [, vars jsonb [, silent bool]])
| boolean
| 指定された JSON パスにあるいずれかのアイテムが、指定された JSON 値と一致するかどうかをチェックします。 | jsonb_path_exists('{"a":[1,2,3,4,5]}', '$.a[*] ? (@ >= $min && @ <= $max)', '{"min":2,"max":4}')
| true
|
jsonb_path_match(target jsonb, path jsonpath [, vars jsonb [, silent bool]])
| boolean
| 指定された json パスにある最初の要素のチェック結果を返します。結果がブール値でない場合、null が返されます。 | jsonb_path_match('{"a":[1,2,3,4,5]}', 'exists($.a[*] ? (@ >= $min && @ <= $max))', '{"min":2,"max":4}')
| true
|
jsonb_path_query(target jsonb, path jsonpath [, vars jsonb [, silent bool]])
| set of jsonb
| 指定された JSON 値に対して、指定された JSON パスにあるすべての JSON アイテムを取得します。 | SELECT * FROM jsonb_path_query('{"a":[1,2,3,4,5]}', '$.a[*] ? (@ >= $min && @ <= $max)', '{"min":2,"max":4}');
| jsonb_path_query
------------------
2
3
4
|
jsonb_path_query_array(target jsonb, path jsonpath [, vars jsonb [, silent bool]])
| jsonb
| 指定された JSON パスにあるすべての JSON アイテムを取得し、結果を配列にラップします。 | jsonb_path_query_array('{"a":[1,2,3,4,5]}', '$.a[*] ? (@ >= $min && @ <= $max)', '{"min":2,"max":4}')
| [2, 3, 4]
|
jsonb_path_query_first(target jsonb, path jsonpath [, vars jsonb [, silent bool]])
| jsonb
| 指定された JSON 値に対して、指定された JSON path にある最初の JSON アイテムを取得します。結果が取得されない場合、NULL が返されます。 | jsonb_path_query_first('{"a":[1,2,3,4,5]}', '$.a[*] ? (@ >= $min && @ <= $max)', '{"min":2,"max":4}')
| 2
|