Skip to content

Instantly share code, notes, and snippets.

@ianstarz
Created July 20, 2014 18:42
Show Gist options
  • Save ianstarz/529d1f1acc12f4a83559 to your computer and use it in GitHub Desktop.
Save ianstarz/529d1f1acc12f4a83559 to your computer and use it in GitHub Desktop.
/* 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