PostgreSQL:row_to_json 具有选择性列
PostgreSQL: row_to_json with selective columns
任务概念和我的问题
使用 Postgres 9.4。我如何将 row_to_json(row)
用于选择性列(而不是整行)?我需要在构建 JSON 时从行构造函数中丢弃一列,但还需要保留列名。
限制
- 不要使用自连接到相同的 table/cte 与选择性列选择
- 不使用外部函数来处理从 json 中删除密钥,之后
我很清楚我可以编写并使用自己的函数从 JSON 中删除一个键,或者在 Postgres 9.5 中有 JSONB 的 -
运算符.但是,我想在没有额外函数调用的情况下预先执行此操作,我很确定这是可能的。
MVCE及解释
正在生成示例数据
CREATE TABLE test_table ( id int, col1 int, col2 int, col3 text );
INSERT INTO test_table VALUES
(1, 23, 15, 'Jessica'), (2, 43, 84, 'Thomas');
1)先试试,简单的row_to_json(row)
,显然不行:
SELECT id, row_to_json(t) FROM test_table t
我需要从行构造函数中丢弃列 id
,以便在将行解析为 json 时不添加它。以上returns:
id | row_to_json
----+-----------------------------------------------
1 | {"id":1,"col1":23,"col2":15,"col3":"Jessica"}
2 | {"id":2,"col1":43,"col2":84,"col3":"Thomas"}
2) 第二次尝试,显式传递列 row_to_json(row(col1, ...))
:
SELECT id, row_to_json(row(col1, col2, col3)) FROM test_table t
但是我丢失了列名(如文档中所述,它全部转换为 fX
,其中 X 是一个数字:
id | row_to_json
----+----------------------------------
1 | {"f1":23,"f2":15,"f3":"Jessica"}
2 | {"f1":43,"f2":84,"f3":"Thomas"}
预期输出
预期输出显然来自 MVCE 中的 (1) 点,但没有 id
键值对:
id | row_to_json
----+-----------------------------------------------
1 | {"col1":23,"col2":15,"col3":"Jessica"}
2 | {"col1":43,"col2":84,"col3":"Thomas"}
似乎创建一个具有所需列名和匹配数据类型的类型,然后将行转换为它就可以解决问题:
CREATE TYPE my_type AS (
col1 int,
col2 int,
col3 text
);
然后通过将行的转换添加到定义的类型来更改我的语句:
SELECT id, row_to_json(cast(row(col1, col2, col3) as my_type)) FROM test_table t;
得出预期的输出:
id | row_to_json
----+-----------------------------------------------
1 | {"col1":23,"col2":15,"col3":"Jessica"}
2 | {"col1":43,"col2":84,"col3":"Thomas"}
但是,有什么方法可以在没有附加类型的情况下构建它吗?
任务概念和我的问题
使用 Postgres 9.4。我如何将 row_to_json(row)
用于选择性列(而不是整行)?我需要在构建 JSON 时从行构造函数中丢弃一列,但还需要保留列名。
限制
- 不要使用自连接到相同的 table/cte 与选择性列选择
- 不使用外部函数来处理从 json 中删除密钥,之后
我很清楚我可以编写并使用自己的函数从 JSON 中删除一个键,或者在 Postgres 9.5 中有 JSONB 的 -
运算符.但是,我想在没有额外函数调用的情况下预先执行此操作,我很确定这是可能的。
MVCE及解释
正在生成示例数据
CREATE TABLE test_table ( id int, col1 int, col2 int, col3 text );
INSERT INTO test_table VALUES
(1, 23, 15, 'Jessica'), (2, 43, 84, 'Thomas');
1)先试试,简单的row_to_json(row)
,显然不行:
SELECT id, row_to_json(t) FROM test_table t
我需要从行构造函数中丢弃列 id
,以便在将行解析为 json 时不添加它。以上returns:
id | row_to_json
----+-----------------------------------------------
1 | {"id":1,"col1":23,"col2":15,"col3":"Jessica"}
2 | {"id":2,"col1":43,"col2":84,"col3":"Thomas"}
2) 第二次尝试,显式传递列 row_to_json(row(col1, ...))
:
SELECT id, row_to_json(row(col1, col2, col3)) FROM test_table t
但是我丢失了列名(如文档中所述,它全部转换为 fX
,其中 X 是一个数字:
id | row_to_json
----+----------------------------------
1 | {"f1":23,"f2":15,"f3":"Jessica"}
2 | {"f1":43,"f2":84,"f3":"Thomas"}
预期输出
预期输出显然来自 MVCE 中的 (1) 点,但没有 id
键值对:
id | row_to_json
----+-----------------------------------------------
1 | {"col1":23,"col2":15,"col3":"Jessica"}
2 | {"col1":43,"col2":84,"col3":"Thomas"}
似乎创建一个具有所需列名和匹配数据类型的类型,然后将行转换为它就可以解决问题:
CREATE TYPE my_type AS (
col1 int,
col2 int,
col3 text
);
然后通过将行的转换添加到定义的类型来更改我的语句:
SELECT id, row_to_json(cast(row(col1, col2, col3) as my_type)) FROM test_table t;
得出预期的输出:
id | row_to_json
----+-----------------------------------------------
1 | {"col1":23,"col2":15,"col3":"Jessica"}
2 | {"col1":43,"col2":84,"col3":"Thomas"}
但是,有什么方法可以在没有附加类型的情况下构建它吗?