jmespath / jmespath.py

JMESPath is a query language for JSON.

Home Page:http://jmespath.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom functions can't get literals, the `types` refer to the json element

spicy-sauce opened this issue · comments

I know you're working on supporting slicing out of the box, but this is just an example:

@functions.signature({"types": ['string']}, {"types": ['number']}, {"types": ['number']})
def _func_slice(self, element, from_index, to_index):
    return str(element)[from_index:to_index]

Test example:

def test_jmespath_slice_expression():
    expression.compile("slice(concat([fname,' ',mname,' ',lname]), 2, 3)")
    assert expression.search({"fname": "john",  "mname": "george", "lname": "smith"}) == "john"

Result:

E       jmespath.exceptions.ParseError: invalid token: Parse error at column 43, token "2" (NUMBER), for expression:
E       "slice(concat([fname,' ',mname,' ',lname]), 2, 3)"
E                                                   ^

Is it indeed not supported or did I miss something?

It also seems the behavior in built in functions, for example:

doesn't work:
abs(-23)

works:
abs(to_number('-22'))

Is it by design or again I miss something?

OK, I figured it out:

abs(`-1`)

Let me know if this is indeed how to do it.

OK, I figured it out:

abs(`-1`)

Let me know if this is indeed how to do it.

@spicy-sauce Yes indeed. You found the secret sauce 😊.