Skip to content

Instantly share code, notes, and snippets.

@booo
Forked from kashif/hs_2json.sql
Created August 26, 2011 14:24
Show Gist options
  • Save booo/1173513 to your computer and use it in GitHub Desktop.
Save booo/1173513 to your computer and use it in GitHub Desktop.
hstore to json function from Ilya Kosmodemiansky
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 || '"' || r.val || '"';
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