kira
kira

Reputation: 513

Can't use JSON_EXTRACT when json name has dash inside

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.

Upvotes: 5

Views: 1210

Answers (1)

sajjad kalantari
sajjad kalantari

Reputation: 893

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  

Upvotes: 9

Related Questions