Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

35 lines (29 sloc) 0.69 kb
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;
{"ok": true}
\.
-- Call twice to test the function cache.
SELECT get_key('ok', data) FROM jsononly;
SELECT get_key('ok', data) FROM jsononly;
Jump to Line
Something went wrong with that request. Please try again.