如何使用 pl/pgsql 将字符文本字段转换为 json?
How to convert a text field of chars to json using pl/pgsql?
需要将 'O T D' 等文本字段转换为 json 输出,例如:
"types": [
"O",
"T",
"D"
],
这是使用 json_agg() 函数导出到 json 的整个查询的子输出。
此代码适用于 t-sql:
JSON_QUERY('["' + replace(rtrim(ltrim(type)), ' ','","') + '"]') as 'types',
即什么pl/pgsql函数类似于t-sqlJSON_QUERY?
没有 PL/pgSQL 函数,但您可以使用 SQL 函数:
SELECT jsonb_agg(x) FROM regexp_split_to_table('O T D', ' ') AS x;
jsonb_agg
-----------------
["O", "T", "D"]
(1 row)
需要将 'O T D' 等文本字段转换为 json 输出,例如:
"types": [
"O",
"T",
"D"
],
这是使用 json_agg() 函数导出到 json 的整个查询的子输出。 此代码适用于 t-sql:
JSON_QUERY('["' + replace(rtrim(ltrim(type)), ' ','","') + '"]') as 'types',
即什么pl/pgsql函数类似于t-sqlJSON_QUERY?
没有 PL/pgSQL 函数,但您可以使用 SQL 函数:
SELECT jsonb_agg(x) FROM regexp_split_to_table('O T D', ' ') AS x;
jsonb_agg
-----------------
["O", "T", "D"]
(1 row)