adriank / ObjectPath

The agile query language for semi-structured data
http://objectpath.org
MIT License
380 stars 93 forks source link

Wierd result for queries on non-object elements #32

Closed ghost closed 9 years ago

ghost commented 9 years ago

Invalid queries return valid results on non-object elements of the tree (e.g. numbers, strings, sequence of strings)

Using the dataset from main page:

$ objectpath -u "http://api.openweathermap.org/data/2.5/box/city?bbox=12,32,15,37,10&cluster=yes"
>>> $.cnt
15
>>> $.cnt.11
15
>>> $.cnt.wtf?
15
>>> $.cod.foo
"200"
>>> $.cod.foo.asd
"200"
>>> $.cod.foo.asd[asd]
"200"
>>> $.cod.foo.bar['???']
"200"

Same applies to sequences of strings:

$ cat > test.json
{"list": [1,2,3,4]}
$ objectpath test.json
>>> $.list
[
  1,
  2,
  3,
  4
]
>>> $.list.123
[
  1,
  2,
  3,
  4
]
>>> $.list.abc
[]

in Python last line yields an empty generator.

adriank commented 9 years ago

Sorry for the late response. I'm travelling much recently.

All of this are intended behaviors, despite it seems odd. OP is designed to be friendly to beginners and powerful for super-users. First group greatly appreciate that invalid queries return what they need, second group (like you) know that the query is invalid anyway. :)

Nevertheless, I need to think whether supporting edge cases vs overall language consistency is more important.

BTW. I'm focusing on JS version of ObjectPath. Python has it's limitations and developing ObjectPath became overly complicated. JS is more low level language and has native support for JSON so its simpler. Check it out if you like at https://github.com/adriank/ObjectPath.JS