Home > Back-end >  Can't use JSON_EXTRACT when json name has dash inside
Can't use JSON_EXTRACT when json name has dash inside

Time:07-18

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  
  • Related