小编luk*_*s83的帖子

postgres json_each 的逆

我在字段中没有 json 文本。我有一行包含列"text","format","keyname","...",我想以 .json 格式将其导出到 json '{"value of keyname column": {"text":"value of text","format":"value of format",... }}'。这在 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;
Run Code Online (Sandbox Code Playgroud)

postgresql json

5
推荐指数
2
解决办法
1824
查看次数

标签 统计

json ×1

postgresql ×1