Skip to content

Instantly share code, notes, and snippets.

View rafaelveloso's full-sized avatar
🎯
Focusing

Rafael Veloso rafaelveloso

🎯
Focusing
View GitHub Profile
@rafaelveloso
rafaelveloso / to_json.sql
Last active August 29, 2015 14:13 — forked from wolph/to_json.sql
CREATE OR REPLACE FUNCTION escape_json (text) RETURNS text AS $$
SELECT replace($1, '''', '\'''); $$ LANGUAGE SQL IMMUTABLE;
CREATE OR REPLACE FUNCTION to_json(text) RETURNS text AS $$
SELECT escape_json($1) $$ LANGUAGE SQL IMMUTABLE;
CREATE OR REPLACE FUNCTION to_json(KEY text, value text) RETURNS text AS $$
SELECT '''' || to_json($1) || ''': ''' || to_json($2) || ''''; $$ LANGUAGE SQL IMMUTABLE;
CREATE OR REPLACE FUNCTION json_access(obj json, path text) RETURNS json AS $$
var obj = JSON.parse(obj);
var paths = path.split(".");
var p;
var ret;
while (p = paths.shift()) {
if (typeof obj[p] == 'undefined'){
obj = null;
break
}