JsonPath: Multiple logical operators need parentheses

From FVue
Jump to: navigation, search

Problem

Parsing of multiple logical AND (&&) or OR (||) operators does not work as one expects:

(byebug) JsonPath.new("data.order.market_type[?(@.id == 1 || @.id == 2 || @.id == 3)]").on(@order)
[{"id"=>1, "code"=>"consumer", "name"=>"Consumer"}]
(byebug) JsonPath.new("data.order.market_type[?(@.id == 3 || @.id == 2 || @.id == 1)]").on(@order)
[]

Environment

  • jsonpath-1.0.4 (ruby gem)

Solution

It appears only the first operator is parsed correctly, and you need explicit parentheses to allow for multiple logical operators::

(byebug) JsonPath.new("data.order.market_type[?(@.id == 3 || (@.id == 2 || @.id == 1))]").on(@order)
[{"id"=>1, "code"=>"consumer", "name"=>"Consumer"}]

This is the case for both AND (&&) and OR (||) operators.

Comments

blog comments powered by Disqus