【发布时间】:2017-06-27 05:14:47
【问题描述】:
我的字段中没有 json 文本。我有一行"text","format","keyname","..." 列,我想以'{"value of keyname column": {"text":"value of text","format":"value of format",... }}' 格式将其导出为json。在 postgresql 中可以吗?
我一直在查看 postgis2geojson 示例,并且已经了解了 row_to_json 函数。 http://www.postgresonline.com/journal/archives/267-Creating-GeoJSON-Feature-Collections-with-JSON-and-PostGIS-functions.html
此外,我一直在查看 postgres json 文档:http://www.postgresql.org/docs/9.3/static/functions-json.html
不幸的是,我没有找到明确满足我需要的功能,但 json_each 却在相反的方向上做了我想要的。一般来说,我认为postgres中的json函数是在假设只有列名可以用作键的情况下设计的。我对吗?有没有我可以用来解决这个问题的 SQL hack?感谢您的帮助。
编辑:
select '"'||keyname||'":"'||row_to_json((select r from(Select text, format,
(select username from my.users where users.id = table.uid) as username,
machinename ) as r ))||'"'
from my.table where id = 1;
【问题讨论】:
-
我认为你要求 PostgreSQL 为你做表示层,PostgresSQL 和其他 SQL 服务器不会进入 - 除非你自己使用存储过程。
-
row_to_json()有什么问题? -
PostgresSQL不是已经通过提供json转换进入表现层了吗?我的意思是有一个功能可以做我想要的,但方向相反。关于 row_to_json():我有一列“keyname”,我希望该列的 VALUES 显示为 json-key。没有设法用 row_to_json() 做到这一点。你呢?
标签: json postgresql