When I use JSON type with fieldname containing dash i get Invalid JSON path expression error
How to repeat:
create table tt (j1 json);
insert into tt values ('{"cache-size":10}'); <-- insert went fine
select JSON_EXTRACT(j1,"$.cache-size") from tt;
ERROR 3143 (42000): Invalid JSON path expression. The error is around character position 12.
CodePudding user response:
Names of keys must be double-quoted strings or valid ECMAScript identifiers
Hyphens are not allowed in ECMAScript identifiers, so the name needs to be quoted in the path expression. This seems to work:
select JSON_EXTRACT(j1, '$."cache-size"') from tt;
id | JSON_EXTRACT(j1, '$."cache-size"')
------ ----------
1 | 10