joshbuddy/jsonpath

Impossible to filter elements of an array with unknown name

Opened this issue · 2 comments

JsonPath.new("$..[?(@.price == 8.95 || @.price == 8.99)].title").on(json)
 => ["Sayings of the Century", "Moby Dick", "Sayings of the Century", "Moby Dick"] 
JsonPath.new("$..[?(@['price'] == 8.95 || @['price'] == 8.99)].title").on(json)
Traceback (most recent call last):
       16: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:35:in `each'
       15: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:35:in `block in each'
       14: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:26:in `each'
       13: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:55:in `handle_wildecard'
       12: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:55:in `each'
       11: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:63:in `block in handle_wildecard'
       10: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:119:in `handle_question_mark'
        9: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:163:in `process_function_or_literal'
        8: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:31:in `parse'
        7: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:45:in `construct_expression_map'
        6: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:45:in `each_with_index'
        5: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:45:in `each'
        4: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:49:in `block in construct_expression_map'
        3: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:99:in `parse_exp'
        2: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:99:in `inject'
        1: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:99:in `each'
NoMethodError (undefined method `price' for 19.95:Float)
JsonPath.new("$..*[?(@.price == 8.95 || @.price == 8.99)].title").on(json)
 => ["Sayings of the Century", "Moby Dick", "Sayings of the Century", "Moby Dick", "Sayings of the Century", "Moby Dick", "Sayings of the Century", "Moby Dick", "Sayings of the Century", "Moby Dick", "Sayings of the Century", "Moby Dick", "Sayings of the Century", "Moby Dick"] 
JsonPath.new("$..*[?(@['price'] == 8.95 || @['price'] == 8.99)].title").on(json)
Traceback (most recent call last):
       16: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:35:in `each'
       15: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:35:in `block in each'
       14: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:26:in `each'
       13: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:55:in `handle_wildecard'
       12: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:55:in `each'
       11: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:63:in `block in handle_wildecard'
       10: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:119:in `handle_question_mark'
        9: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/enumerable.rb:163:in `process_function_or_literal'
        8: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:31:in `parse'
        7: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:45:in `construct_expression_map'
        6: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:45:in `each_with_index'
        5: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:45:in `each'
        4: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:49:in `block in construct_expression_map'
        3: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:99:in `parse_exp'
        2: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:99:in `inject'
        1: from /home/vladimir/.rvm/gems/ruby-2.7.0/gems/jsonpath-1.1.0/lib/jsonpath/parser.rb:99:in `each'
NoMethodError (undefined method `price' for 19.95:Float)

Hey, I'd happily accept a pr to fix this behavior

Well, maybe I'll do that if you accept a full rewrite of the parser. I think an implementation using parslet would be nice. Here is an example of json5 parser implemented with parslet.