Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
38 lines (35 sloc) 769 Bytes
CREATE FUNCTION valid_json(json text) RETURNS boolean
LANGUAGE plv8 IMMUTABLE STRICT
AS $$
try {
JSON.parse(json);
return true;
} catch(e) {
return false;
}
$$;
CREATE DOMAIN json AS text
CONSTRAINT json_check CHECK (valid_json(VALUE));
CREATE FUNCTION get_key(key text, json_raw text) RETURNS json
LANGUAGE plv8 IMMUTABLE STRICT
AS $$
var val = JSON.parse(json_raw)[key];
var ret = {};
ret[key] = val;
return JSON.stringify(ret);
$$;
CREATE TABLE jsononly (
data json
);
COPY jsononly (data) FROM stdin;
-- Call twice to test the function cache.
SELECT get_key('ok', data) FROM jsononly;
get_key
-------------
{"ok":true}
(1 row)
SELECT get_key('ok', data) FROM jsononly;
get_key
-------------
{"ok":true}
(1 row)