jmespath / jmespath.py

JMESPath is a query language for JSON.
http://jmespath.org
MIT License
2.19k stars 181 forks source link

to_number does not correctly handle floating point numbers without dots #120

Closed jtprobst closed 7 years ago

jtprobst commented 7 years ago

The JMESPath specification of the to_number function states that everything matching the json-number production must be supported. However, this implementation will handle floating point numbers incorrectly if they do not contain a dot.

Please consider the following example

{"x": "1e+21"}

When searched with the JMESPath

x | to_number(@)

this implementation will return None since the number is not recognized as such. The parser will only try parsing a floating point number if it contains a dot, as can be seen in the code

Therefore, the following (incorrect) behavior can be observed

$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import jmespath
>>> data = {"x": "1e+21"}
>>> jmespath.search("x | to_number(@)", data)
>>> 

Please note the None return value which is not printed by the REPL.

The JSON standard allows such numbers, as per http://www.json.org/ where the grammar allows a special case of number number := int exp where exp := e digits and e := 'e+'.

My package version is 0.9.1.

jamesls commented 7 years ago

Thanks for reporting. This is fixed in #134.

jtprobst commented 7 years ago

@jamesls Thank you very much for your efforts! It is much appreciated!