Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading