Created
September 29, 2015 20:09
-
-
Save bfcoder/7843c0e1bbdb3923dfed to your computer and use it in GitHub Desktop.
PostgreSQL (9.2+) : How to return an empty json array from an empty resultset.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* will return null::json on empty resultset */ | |
SELECT array_to_json(array_agg(row_to_json(t))) FROM t; | |
/* | |
will return '[]' on empty resultset, | |
null::json seems to be managed the same way than sql NULL by COALESCE() | |
*/ | |
SELECT COALESCE(array_to_json(array_agg(row_to_json(t))), '[]') FROM t; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment