{"id":402,"date":"2024-07-27T12:26:32","date_gmt":"2024-07-27T18:26:32","guid":{"rendered":"https:\/\/www.businesstechnologyassociates.com\/blog\/?p=402"},"modified":"2024-07-27T12:26:32","modified_gmt":"2024-07-27T18:26:32","slug":"mysql-jason_value-fails-with-a-key-starting-with-a-number","status":"publish","type":"post","link":"https:\/\/www.businesstechnologyassociates.com\/blog\/2024\/07\/mysql-jason_value-fails-with-a-key-starting-with-a-number\/","title":{"rendered":"MySQL JASON_VALUE fails with a key starting with a number"},"content":{"rendered":"<pre>SELECT JSON_VALUE('{\"dateTime\": \"2024-07-21 08:11:07 -0600\", \"60SecInches\": 0.00, \"DailyInches\": 0.01, \"TotalInches\": 10.61}', \"$.60SecInches\");<\/pre>\n<p>MySQL said: #3143 &#8211; Invalid JSON path expression. The error is around character position 13.\u00a0 That seems to point to the end of this string.<\/p>\n<pre>\"$.60SecInches\"<\/pre>\n<p>If I remove the &#8220;60&#8221; from the key, the above SELECT works. But the following works as well.<\/p>\n<pre>SELECT JSON_KEYS('{\"dateTime\": \"2024-07-21 08:11:07 -0600\", \"60SecInches\": 0.00, \"DailyInches\": 0.01, \"TotalInches\": 10.61}');\r\n\r\n[\"dateTime\", \"60SecInches\", \"DailyInches\", \"TotalInches\"]<\/pre>\n<p>According to JSON.org, a string is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. Any valid string can be used as a JSON key.<\/p>\n<p>I tried backslashes in various places, but that did not work.<\/p>\n<p>The solution is the key name must be <strong>separately<\/strong> enclosed in double-quotes and the whole path must be enclosed with single-quotes as<\/p>\n<pre>'$.\"60SecInches\"'<\/pre>\n<p>This can be used for other &#8220;special characters&#8221; as well. For example &#8220;*&#8221; or &#8220;#&#8221; or number only keys.\u00a0 I am not sure why a number is a special character, but it seems to be in this case.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SELECT JSON_VALUE(&#8216;{&#8220;dateTime&#8221;: &#8220;2024-07-21 08:11:07 -0600&#8221;, &#8220;60SecInches&#8221;: 0.00, &#8220;DailyInches&#8221;: 0.01, &#8220;TotalInches&#8221;: 10.61}&#8217;, &#8220;$.60SecInches&#8221;); MySQL said: #3143 &#8211; Invalid JSON path expression. The error is around character position 13.\u00a0 That seems to point to the end of this string. &#8220;$.60SecInches&#8221; If I remove the &#8220;60&#8221; from the key, the above SELECT works. But the following works as &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.businesstechnologyassociates.com\/blog\/2024\/07\/mysql-jason_value-fails-with-a-key-starting-with-a-number\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;MySQL JASON_VALUE fails with a key starting with a number&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,3],"tags":[],"class_list":["post-402","post","type-post","status-publish","format-standard","hentry","category-mysql","category-technology"],"_links":{"self":[{"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/posts\/402","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/comments?post=402"}],"version-history":[{"count":6,"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/posts\/402\/revisions"}],"predecessor-version":[{"id":408,"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/posts\/402\/revisions\/408"}],"wp:attachment":[{"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/media?parent=402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/categories?post=402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.businesstechnologyassociates.com\/blog\/wp-json\/wp\/v2\/tags?post=402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}