Hi there
I sometimes need to iterate over a json array and do so by using table(json_to_array(...))
. However, when I want to use the value, it is often wrapped in quotes, due to the value being a string. When I wish to utilize the value, it’s an issue that’s it’s wrapped in quotes. I’ve tried a multitude of ways, to get the actual value, like json_extract_string(table_col, '.')
, but with no luck.
As a small example, I’ve got some URLs I need to look through and I’m getting multiple substring to search for:
SELECT links.url,
x.table_col
FROM (SELECT 'https://www.singlestore.com/hello' url FROM dual
union all
SELECT 'https://www.singlestore.com/world' url FROM dual
) links
JOIN TABLE(JSON_TO_ARRAY('["hello", "cats", "dogs"]')) x
on (instr(links.url, x.table_col) > 0);
The above query returns no rows, but I would expect to get a row for https://www.singlestore.com/hello
.
So what I usually end up doing, is building a new json object and then extracting the value: json_extract_string(json_build_object('val', x.table_col), 'val'))
, but I really do believe that this is not the best way to go about it. Doing that however does make the query work:
SELECT links.url,
x.table_col
FROM (SELECT 'https://www.singlestore.com/hello' url FROM dual
union all
SELECT 'https://www.singlestore.com/world' url FROM dual
) links
JOIN TABLE(JSON_TO_ARRAY('["hello", "cats", "dogs"]')) x
on (instr(links.url, json_extract_string(json_build_object('val', x.table_col), 'val')) > 0);
+-----------------------------------+-----------+
| url | table_col |
+-----------------------------------+-----------+
| https://www.singlestore.com/hello | "hello" |
+-----------------------------------+-----------+
I would really like if there was a way to do json_extract_string(table_col, '.')
or maybe just table_col::$
. Maybe there already is, but I’ve just failed to find it in the documentation.