Created
June 17, 2011 10:26
-
-
Save kashif/1031178 to your computer and use it in GitHub Desktop.
hstore to json function from Ilya Kosmodemiansky
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 FUNCTION public.hs_2json(hs hstore) RETURNS text AS $$ | |
DECLARE | |
rv text; | |
r record; | |
BEGIN | |
rv:=''; | |
for r in (select key, val from each(hs) as h(key, val)) loop | |
if rv<>'' then | |
rv:=rv||','; | |
end if; | |
rv:=rv || '"' || r.key || '":'; | |
rv:=rv || '"' || regexp_replace(regexp_replace(r.val,$$\\$$,$$\\\\$$,'g'),'"', $$\"$$,'g') || '"'; | |
end loop; | |
return '{'||rv||'}'; | |
END; | |
$$ LANGUAGE plpgsql IMMUTABLE; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You missed the count in line 6 ;)