to_jsonb()
是一个用于将SQL值转换为jsonb
的系统函数。
to_jsonb()
已在 PostgreSQL 9.5 中添加。
示例
to_jsonb()
的基本用法示例
postgres=# SELECT to_jsonb('hello, world'::text); to_jsonb ---------------- "hello, world" (1 row) postgres=# SELECT to_jsonb(row(97, 'foo', ARRAY[1,2,3])); to_jsonb ------------------------------------------ {"f1": 97, "f2": "foo", "f3": [1, 2, 3]} (1 row)
由于 to_jsonb()
接受 anyelement
作为其参数,因此歧义数据类型需要提供显式转换。
postgres=# SELECT to_jsonb('hello, world'); ERROR: could not determine polymorphic type because input has type unknown
参考资料
- PostgreSQL文档: JSON创建函数