Skip to content

Instantly share code, notes, and snippets.

@xwmx
Last active December 25, 2015 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xwmx/7061260 to your computer and use it in GitHub Desktop.
Save xwmx/7061260 to your computer and use it in GitHub Desktop.
json pl/v8 functions (not currently tested)
CREATE or REPLACE FUNCTION
json_get(data json, prop text) RETURNS JSON as $$
prop = prop.split('.');
for (var i = 0, len = prop.length; i < len - 1; i++) {
data = data[prop[i]];
}
return data[prop[len - 1]];
$$ LANGUAGE plv8 STABLE STRICT;
CREATE or REPLACE FUNCTION
json_set(data json, prop text, value json) RETURNS JSON as $$
prop = prop.split('.');
for (var i = 0, len = prop.length; i < len - 1; i++) {
data = data[prop[i]];
}
data[prop[len - 1]] = value;
return data;
$$ LANGUAGE plv8 STABLE STRICT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment