jmespath / jmespath.py

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

numeric comparing not working #196

Closed ho4040 closed 4 years ago

ho4040 commented 4 years ago

i'm using 0.9.5 version. let's suppose document like below

[ 
 {"name":"goblin", "states":[
   {"name":"running", "defense":0},
   {"name":"guard", "defense":1}
 ]},
 {"name":"ogre", "states":[
   {"name":"running", "defense":1},
   {"name":"guard", "defense":2}
 ]}
]

the query [].states[] | [?name=='running'] is works fine. but query [].states[] | [?defense>0] is not working.

It shows ParseError.

jamesls commented 4 years ago

You have to specify it was a "literal value" (surround it with ` chars) to denote that it's a number type, similar to how you have to use quotes to denote it's a literal string.

$ echo '[
 {"name":"goblin", "states":[
   {"name":"running", "defense":0},
   {"name":"guard", "defense":1}
 ]},
 {"name":"ogre", "states":[
   {"name":"running", "defense":1},
   {"name":"guard", "defense":2}
 ]}
]' | jp.py '[].states[] | [?defense > `0`]'

[
    {
        "name": "guard",
        "defense": 1
    },
    {
        "name": "running",
        "defense": 1
    },
    {
        "name": "guard",
        "defense": 2
    }
]