Created
November 9, 2020 06:49
-
-
Save vuon9/baa05fc074e28047881be0e041c195bf to your computer and use it in GitHub Desktop.
JSON_EXTRACT_C func for parsing json string in mysql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELIMITER $$ | |
DROP FUNCTION IF EXISTS `json_extract_c`$$ | |
CREATE FUNCTION `json_extract_c`( | |
details TEXT, | |
required_field VARCHAR (255) | |
) RETURNS TEXT CHARSET utf8 | |
BEGIN | |
SET details = TRIM(LEADING '{' FROM TRIM(details)); | |
SET details = TRIM(TRAILING '}' FROM TRIM(details)); | |
RETURN TRIM( | |
BOTH '"' FROM SUBSTRING_INDEX( | |
SUBSTRING_INDEX( | |
SUBSTRING_INDEX( | |
details, | |
CONCAT( | |
'"', | |
SUBSTRING_INDEX(required_field,'$.', - 1), | |
'":' | |
), | |
- 1 | |
), | |
',"', | |
1 | |
), | |
':', | |
-1 | |
) | |
) ; | |
END$$ | |
DELIMITER ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment