Created
January 20, 2012 22:14
-
-
Save kashif/1649904 to your computer and use it in GitHub Desktop.
Another hstore to JSON function by Andrew Dunstan
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
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