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,
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.
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).
Specially the
row_to_jsonconverts a record into a JSON object and thearray_to_jsonturns arrays into JSON arrays.For example, both functions can be combined to easily turn the results of a
SELECTquery into JSON: