Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OR operator in JSONPath?

Tags:

json

jsonpath

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?

like image 411
Andrew G Avatar asked May 27 '14 19:05

Andrew G


People also ask

Can we use or operator in JSON?

The $and, $or, $not, and $nor logical operators are supported.

What is array slice operator in JsonPath?

: operator is the array slice operator, so you can slice collections using the syntax [start:end:step] to return a subcollection of a collection. ( ) operator lets you pass a script expression in the underlying implementation's script language. It's not supported by every implementation of JSONPath, however.

How do I specify JsonPath?

You use a JSONPath expression to traverse the path to an element in the JSON structure. You start at the root node or element, represented by $, and reach the required element in the JSON structure to extract data from it. You can use either the dot-notation or the bracket-notation to form the expressions.

What is the output of JsonPath () method?

3.3. JsonPath also has functions that we can use at the end of a path to synthesize that path's output expressions: min(), max(), avg(), stddev() and length(). Finally, we have filters. These are boolean expressions to restrict returned lists of nodes to only those that calling methods need.


2 Answers

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

$..log[?(@.severity == 'WARN' || @.severity == 'Error')]
like image 138
elyas-bhy Avatar answered Oct 11 '22 01:10

elyas-bhy


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.

like image 34
sirugh Avatar answered Oct 10 '22 23:10

sirugh