19

Using a single JSONPath expression alone, is it possible to do some kind of 'OR' or '||' operator. For example, these two JSONPath boolean expressions work to check the severity of a log JSON file:

$..log[?(@.severity == 'WARN')]

$..log[?(@.severity == 'Error')]

But I'd like to do something logically similar to:

$..log[?(@.severity == 'WARN' or @.severity == 'Error')] //this is not correct 

Is there any way to do this?

Andrew G
  • 412
  • 1
  • 5
  • 14

3 Answers3

24

If you are using Goessner's parser, you can use the || operator within your expression as follows:

$..log[?(@.severity == 'WARN' || @.severity == 'Error')]
elyas-bhy
  • 772
  • 1
  • 14
  • 23
9

From the JSONPath page:

[,] - Union operator in XPath results in a combination of node sets. JSONPath allows alternate names or array indices as a set.

Try

$..log[?(@.severity == 'WARN'), ?(@.severity == 'Error')]

Edit: Looks like there is an open issue for logical AND and OR operators in which they state that the operators are not yet supported by JSONPath.

sirugh
  • 324
  • 2
  • 11
  • 6
    Just so others don't get confused: the OR operator is not yet supported by JSONPath and the above code will not work. – Andrew G May 27 '14 at 20:04
1

Thank you for your answers. In my use case I got it working by combining the two answers from @sirugh and @elyas-bhy.

$..log[?(@.severity == 'WARN') || ?(@.severity == 'Error')]
miqueloi
  • 688
  • 4
  • 13