jmespath / jmespath.py

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

max/min/max_by/min_by functions should support strings #65

Closed jamesls closed 10 years ago

jamesls commented 10 years ago

I think the current inconsistently is confusing with sort/sort_by. You can sort/sortby with arrays of strings, but not with the min/max_ functions. I'd propose updating the spec to support strings:

# Works
>>> jmespath.search('sort(@)', ['b', 'a', 'd', 'c'])
['a', 'b', 'c', 'd']

# Fails
>>> jmespath.search('max(@)', ['b', 'a', 'd', 'c'])
jmespath.exceptions.JMESPathTypeError: In function max(), invalid type for value: b, expected one of: ['array-number'], received: "str"

cc @mtdowling

mtdowling commented 10 years ago

Can you provide more information on how values are compared? For example, PHP's max function lays out exactly how values are compared when they're mixed: http://php.net/manual/en/function.max.php

jamesls commented 10 years ago

It wouldn't compare mixed values. The intent here was to bring parity to sort/min/max and sort_by/min_by/max_by. Currently sort* supports an array of numbers or an array of strings. The min/max should have the same behavior. Trying to mix types should have the same behavior as sort/sort_by which results in an error.