Quantcast
Channel: Planet PostgreSQL
Viewing all articles
Browse latest Browse all 9659

Andrew Dunstan: Another transformation

$
0
0
A few people have mentioned an hstore to JSON transformation to me. I don't think it's possible to get a perfect fit, but something like this gets fairly close, and will work for many cases:
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$;

Viewing all articles
Browse latest Browse all 9659

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>