为什么 Postgres jsonb_agg 函数将输出包装到额外的 JSON 字段中?
SELECT jsonb_agg(t) AS json_array
FROM (SELECT jsonb_set('[{"f1": "1","f2": null}, 2, null, 3]', '{0,f1}', '"DELETEME"',true)) t
它返回这个:
[{"jsonb_set": [{"f1": "DELETEME", "f2": null}, {"f3": "1", "f4": null}]}]
如何避免此类行为?因为我需要它返回:
[{"f1": "DELETEME", "f2": null}, {"f3": "1", "f4": null}]
您正在将整个表别名
t
转换为 json,而不仅仅是那一列。将 t
更改为 t.jsonb_set
。这有效:
=# SELECT jsonb_agg(t.jsonb_set) AS json_array
FROM (SELECT jsonb_set('[{"f1": "1","f2": null}, 2, null, 3]', '{0,f1}', '"DELETEME"',true)) t;
json_array
------------------------------------------------
[[{"f1": "DELETEME", "f2": null}, 2, null, 3]]
(1 row)