jmespath / jmespath.js

Javascript implementation of JMESPath, a query language for JSON
http://jmespath.org
Other
783 stars 97 forks source link

Numeric literals can only wrapped with backticks, not single quotes #85

Open slandelle opened 2 years ago

slandelle commented 2 years ago

Hi,

According to the JMESPath specification:

literal           = "`" json-value "`"

This implementation doesn't abide to the specification and allows literals to be also wrapped with single quotes.

{
"status":"GOOD",
"fruits":[
  {
    "name":"Apple",
    "details": {
       "size":500
      }
  },
  {
    "name":"Cherry",
    "details": {
        "size":100
      }
  }]
}
fruits[?details.size >= `500`]
fruits[?details.size >= '500']

On the online evaluator on https://jmespath.org/ (using this implementation), both following expressions work. Only the former should. The latter should return an empty array.

For the record, the Java implementation works as expected.

Regards

springcomp commented 2 years ago

JMESPath used to support strings using this syntax: `this was a string`. However, JEP-12 made this syntax obsolete.

Now, the grammar is much clearer. You can only have:

Your question is raised in the context of numeric literals. JMESPath does not support JSON numbers in its grammar. So those must be input using raw-string literals. Thus:

Comparisons between numbers and strings is currently undefined in the grammar as ordering operators are only valid between two numbers. However, popular implementations do make some attempt to cast the string to a number before performing the comparison, so it mostly works even though it is not standards-compliant.