Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DELIMITER ;;
- CREATE FUNCTION getValueFromJSON(object TEXT, path TINYTEXT) RETURNS text
- NO SQL
- DETERMINISTIC
- begin
- declare element tinytext;
- declare value_ text;
- if locate('.', path) then
- set element = substring(path, 1, locate('.', path) - 1);
- else
- set element = path;
- end if;
- set value_ = getValueFromJSONWithoutPath(object, element);
- if element != path then
- return getValueFromJSONWithoutPath(value_, substring(path, length(element) + 2));
- else
- return value_;
- end if;
- end ;;
- CREATE FUNCTION getValueFromJSONWithoutPath(object TEXT, element TINYTEXT) RETURNS text
- NO SQL
- DETERMINISTIC
- begin
- declare startPosition int;
- declare endPosition int;
- declare quote_ int;
- declare comma int;
- declare bracket int;
- declare delimiter_ char;
- set startPosition = locate(concat('"', element, '":'), object);
- if startPosition = 0 then
- return null;
- else
- set startPosition = startPosition + length(element) + 3;
- end if;
- set quote_ = locate('"', object, startPosition);
- if quote_ = startPosition then
- set startPosition = startPosition + 1;
- set endPosition = locate('"', object, quote_ + 1);
- else
- set delimiter_ = substring(object, startPosition, 1);
- if delimiter_ = '[' then
- set endPosition = locate(']', object, startPosition) + 1;
- else
- if delimiter_ = '{' then
- set endPosition = locate('}', object, startPosition) + 1;
- else
- set comma = locate(',', object, startPosition);
- set bracket = locate('}', object, startPosition);
- if comma != 0 and comma < bracket then
- set endPosition = comma;
- else
- set endPosition = bracket;
- end if;
- end if;
- end if;
- end if;
- RETURN substring(object, startPosition, endPosition - startPosition);
- end ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement