Skip to content

Instantly share code, notes, and snippets.

@kashif
Created January 20, 2012 22:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kashif/1649904 to your computer and use it in GitHub Desktop.
Save kashif/1649904 to your computer and use it in GitHub Desktop.
Another hstore to JSON function by Andrew Dunstan
create or replace function hstore_to_json(h hstore) returns text language sql
as $f$
select '{' || array_to_string(array_agg(
'"' || regexp_replace(key,E'[\\"]',E'\\\&','g') || '":' ||
case
when value is null then 'null'
when value ~ '^true|false|(-?(0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?)$' then value
else '"' || regexp_replace(value,E'[\\"]',E'\\\&','g') || '"'
end
),',') || '}'
from each($1)
$f$;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment