我希望我不会在这里遗漏一些很明显的东西, 我想从postgres函数获取JSON输出(我想很多其他人都已经需要这个了),我很乐意在我的服务器上安装contrib函数的扩展,
I hope I am not missing something very obvious here, I want to get JSON output from a postgres function (I imagine many other had already needed this) and I'd be happy to install an extension of contrib functions on my server,
有什么方法可以从sql或plpgsql函数(或在db-server-side python的帮助下)获取JSON输出吗?具体来说,我想将record[]结果作为JSON获取.
Is there any way to get JSON output from sql or plpgsql functions (or with help of db-server-side python)? Specifically I want to get my record[] results as JSON.
推荐答案自 PostgreSQL 9.2 ,它在最新版本中具有许多其他功能(例如: PostgreSQL 0.4中的JSON函数).
There is built-in support for JSON since PostgreSQL 9.2 and it has increased with many other features in more recent versions(For example: JSON functions in PostgreSQL 0.4).
特别地,row_to_json将记录转换为JSON对象,而array_to_json将数组转换为JSON数组.
Specially the row_to_json converts a record into a JSON object and the array_to_json turns arrays into JSON arrays.
例如,可以将这两个函数结合起来以轻松地将SELECT查询的结果转换为JSON:
For example, both functions can be combined to easily turn the results of a SELECT query into JSON:
SELECT array_to_json(array_agg(row_to_json(t))) FROM (SELECT col1, col2, col3 FROM example_table) t更多推荐
Postgresql中的JSON输出
发布评论