Well with the help of a friend of mine I was able write a function that basically rips apart the string and returns the value of the key you were looking for, so returning that function as a variable you are able to sort on it now. It has a few issues I can see, including probably not working on multi-level depths (although have not tested) and stripping valid double quotes in the value if they were at the edges but I can fix that just need more IFs and substrings to replace the trims on the return.
DELIMITER $$
DROP FUNCTION IF EXISTS `MYTABLE`.`json_value`$$
CREATE FUNCTION `MYTABLE`.`json_value`( colValue TEXT, keyName varchar(32) ) RETURNS varchar(255) CHARSET latin1
BEGIN
DECLARE output VARCHAR(255) DEFAULT null;
DECLARE cur_position INT DEFAULT 1;
DECLARE remainder TEXT;
DECLARE cur_string VARCHAR(1000);
DECLARE delimiter_length TINYINT UNSIGNED;
DECLARE delimiter_char CHAR(1);
DECLARE tmp text;
SET remainder = colValue;
SET delimiter_length = 1;
SET tmp = null;
SET delimiter_char = ",";
WHILE CHAR_LENGTH(remainder) > 0 AND cur_position > 0 DO
SET cur_position = INSTR(remainder, delimiter_char);
IF cur_position = 0 THEN
SET cur_string = remainder;
ELSE
SET cur_string = LEFT(remainder, cur_position - 1);
END IF;
IF TRIM(cur_string) != '' THEN
IF SUBSTRING_INDEX(cur_string, ':', 1) = CONCAT('{"',keyName,'"') THEN
SET tmp = SUBSTRING_INDEX(cur_string, ':', -1);
END IF;
END IF;
SET remainder = CONCAT('{',SUBSTRING(remainder, cur_position + delimiter_length));
END WHILE;
RETURN TRIM(BOTH '"' FROM (TRIM(TRAILING '}' FROM (TRIM(LEADING '{' FROM TRIM(tmp))))));
END$$
DELIMITER ;
So to use it
data on diff rows column "param":
{"r":0,"addto":1,"sda":"34"}
{"r":"1","addto":"HELLO \"WORLD\""}
{"addto":1,"removable":0}
{"addto":"0","removable":0}
select json_value(param, "addto") as test from MYTABLE order by test desc;
will return
HELLO \"WORLD\
1
1
(notice how the last slash was trimmed off)
I thought this was pretty cool, even if it is not the most efficient way I didn't even think it was going to be possible via all mysql.
Now I am not sure for filtering if it would be faster to use this and then run a LIKE on the value or my regular expression check. Gona have to test out execution times when I get more data 🙂 I am thinking the regex will be faster although.