OR operator in JSONPath? - json

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?

If you are using Goessner's parser, you can use the || operator within your expression as follows:
$..log[?(#.severity == 'WARN' || #.severity == 'Error')]

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.

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')]

Related

How to use ternary operator for colpan in angular js?

Is this the correct way to set the colspan value with the ternary operator?
colspan="((true && false) ? 9 : (true || false) ? 8 : 7)"
I'm not getting the exact result with this.
this is the output I'm getting.
Sorry for the outlines.
I'm expecting something like this
Note:-
columns are dynamic, which is why I need to add a ternary operator for this.

What is the technical term for a programming language's operator evaluation order?

Several procedures such as array destructuring in JavaScript or collection manipulation in Python have prompted me to evaluate an object's property or method to check if it even exists before proceeding, often resulting in the following pattern:
var value = collection.length
if value != null {
if value == targetValue {
/* do something */
}
}
In an attempt to make "cleaner" code I want to do something like:
if value != null && value == targetValue {
/* do something */
}
or with a ternary operator:
var value = collection.length != null ? collection.length : 0
However, I'm never sure if the compiler will stop evaluating as soon as it resolves the first comparison to null, or if it'll keep going and produce an error. I can of course do small unit tests to find out but I'd prefer if I knew the right term to look up in any language's documentation. What is this term, or is it perhaps the same in all languages?
This is known as Short Circuit Evaluation . It's quite consistent between languages.
In most languages, && will only evaluate the second argument if the first was true, and || will only evaluate its second if the first was false.

the if statement in TCL

I have a question about if statement in tcl of the following code:
if {(($number == 1)&&($name == "hello")) || (($number == 0)&&($name == "yes"))} {
#do something here
}
The above code works, but if I wrote it down like this:
if {{$number == 1 && $name == "hello"} || {$number == 0&&$name == "yes"}} {
#do something here
}
It complains that the $number is expected to be a boolean, why? Is the second one not a valid expression? How to correct it?
Braces, {}, and parentheses, (), are not interchangeable in Tcl.
Formally, braces are (with one exception) a kind of quoting that indicates that no further substitutions are to be performed on the contents. In the first case above, this means that that argument is delivered to if without substitution, which evaluates it as an expression. The expression sub-language has a strongly-analogous brace interpretation scheme to general Tcl; they denote a literal value with no further substitutions to perform on it.
By contrast, parentheses are mostly not special in Tcl. The exceptions are in the names of elements of arrays (e.g., $foo(bar)), in the expression sublanguage (which uses them for grouping, as in mathematical expressions all over programming) and in the regular expression sublanguage (where they are a different type of grouping and a few other things). It is entirely legal to use parentheses — balanced or otherwise — as part of a command name in Tcl, but you might have your fellow programmers complaining at you anyway for writing confusing code.
The Specifics
In this specific case, the test expression of this if:
if {{$number == 1 && $name == "hello"} || {$number == 0&&$name == "yes"}} {...}
is parsed into:
blah#1 LOGICAL_OR blah#2
where each blah is a literal. Unfortunately, blah#1 (which is exactly equal to $number == 1 && $name == "hello") has no boolean interpretation. (Nor does blah#2 but we never bother to consider that.) Things are definitely going very wrong here!
The simplest fix is to change those bogus braces back to parentheses:
if {($number == 1 && $name == "hello") || ($number == 0&&$name == "yes")} {...}
I bet this is what you originally wanted.
Warning: Advanced Topic
However, the other fix is to add in a little extra:
if {[expr {$number == 1 && $name == "hello"}] || [expr {$number == 0&&$name == "yes"}]} {...}
This is normally not a good idea — extra bulk for no extra gain — but it makes sense where you're trying to use a dynamically-generated expression as a test condition. Do not do this unless you're really really certain you need to do this! I mean it. It's a very advanced technique that you hardly ever need, and there's often a better way to do your overall goal. If you think you might need it, for goodness' sake ask here on SO and we'll try to find a better way; there's almost always one available.
I think the error message you are getting does not mean that $number has to be a boolean (I got the message expected boolean value but got "$number == 1 && $name == "hello"").
It means that the string $number == 1 && $name == "hello" is not a boolean value - which is definitely true.
If you use curly braces in your if expression those strings are not evaluated but are simply interpreted as they are - as a string of characters.
In short: if uses a special "mini language" for its condition script — the same understood by the expr command. This is stated in the if manual page:
The if command evaluates expr1 as an expression (in the same way that expr evaluates its argument).
In contrast to Tcl itself, which is quite LISP-like and/or Unix shell-like, that "expr mini language" is way more "traditional" in the sense it feels like C.
In ($number == 1) number is assigned 1 and the comparison is made.Ex: 1==1 here output is Boolean.
But in {$number == 1 && $name == "hello"} $number is not assigned because of flower bracket $number is compared with 1 so output obtained is not Boolean.

Is there a nesting levels limit with expressions used in SSIS packages?

Working in SQL Server 2008. My first stab at an SSIS script and I need to emulate some if/then conditional logic written in VB.net. I couldn't find any previous questions dealing with nested conditions in expressions and believe I'm following what I've been able to uncover via google on nested conditions in a derived column.
I'm receiving an error while attempting to use nested conditions in the derived column transformation editor. The error I'm receiving indicates that SSIS could not parse my expression. The actual exception: "Exception from HRESULT: 0xC0204006 (Microsoft.SqlServer.DTSPipelineWrap)"
Questions for which the answers might immediately answer my question (and create a new problem):
is there a nesting levels limit?
can nesting be performed in the condition1 portion of [expression] ? [condition1] : [condition2]
I'll give two snippets, the first is what I'm actually inserting, the second is a more reader-friendly version. Hopefully someone can point out my error.
Not sure that it has bearing, but please note that [BusArea] is a column derived in a previous step.
actual expression:
[BusArea] == "CCC" || [BusArea] == "NBU" || [BusArea] == "CA" ? (ISNULL([CASE_MORG]) or TRIM([CASE_MORG]) == "" ? ( ISNULL([TRX_MORG]) or TRIM([TRX_MORG]) == "" ? NULL(DT_WSTR,50) : [TRX_MORG]) : [CASE_MORG]) : (ISNULL([CASE_AGT]) or TRIM([CASE_AGT]) == "" ? ( ISNULL([TRX_AGT]) or TRIM([TRX_AGT]) == "" ? NULL(DT_WSTR,50) : [TRX_AGT]) : [CASE_AGT])
formatted for easier reading:
[BusArea] == "CCC" || [BusArea] == "NBU" || [BusArea] == "CA" ?
(ISNULL([CASE_MORG]) or TRIM([CASE_MORG]) == "" ?
( ISNULL([TRX_MORG]) or TRIM([TRX_MORG]) == "" ?
NULL(DT_WSTR,50)
: [TRX_MORG]
)
: [CASE_MORG]
)
: (ISNULL([CASE_AGT]) or TRIM([CASE_AGT]) == "" ?
( ISNULL([TRX_AGT]) or TRIM([TRX_AGT]) == "" ?
NULL(DT_WSTR,50)
: [TRX_AGT]
)
: [CASE_AGT]
)
I don't think there is any limit with nesting conditions. Even if there is one, I don't think we will reach the limit in the packages that we create handle our business processes.
You almost got everything correct. The issue with your conditional statement is that you have used or instead of ||
I copied your exact statement and pasted in Derived Transformation within a Data Flow task and got an error because the package couldn't parse the expression. I replaced all the or's with correct Logical OR (||) operator and the expression evaluated correctly.

LINQ subquery IN

I'm a newbie with the IQueryable, lambda expressions, and LINQ in general. I would like to put a subquery in a where clause like this :
Sample code :
SELECT * FROM CLIENT c WHERE c.ETAT IN (
SELECT DDV_COLUMN_VAL FROM DATA_DICT_VAL
WHERE TBI_TABLE_NAME = 'CLIENT' AND DD_COLUMN_NAME = 'STATUS'
AND DDV_COLUMN_VAL_LANG_DSC_1 LIKE ('ac%'))
How do I translate this in LINQ ?
var innerquery = from x in context.DataDictVal
where x.TbiTableName == myTableNameVariable
&& x.DdColumnName == "Status"
&& x.DdbColumnValLangDsc1.StartsWith("ac")
select x.DdvColumnVal;
var query = from c in context.Client
where innerquery.Contains(c.Etat)
select c;
from c in db.Client
where (from d in db.DataDictVal
where d.TblTableName == "Client"
&& d.DDColumnName == "Status"
&& dd.DdvColumnValLandDsc1.StartsWith("ac"))
.Contains(c.Etat)
select c;
If you are new to Linq, you absolutely need two essential tools. The first is a tool that converts most T-SQL statements to Linq called Linqer (http://www.sqltolinq.com/). This should take care of the query in your question. The other tool is LinqPad (http://www.linqpad.net/). This will help you learn Linq as you practice with queries.
I often use Linqer to convert a T-SQL query for me, and then use LinqPad to fine tune it.
Same example with Linq method syntax:
var innerquery = dbcontext.DataDictVal
.where(x=> x.TbiTableName == myTableNameVariable
&& x.DdColumnName == "Status"
&& x.DdbColumnValLangDsc1.StartsWith("ac"))
.select(x=>x.DdvColumnVal)
var query = dbcontext.Client
.where( c=>innerquery.Contains(c.Etat))
Note:
Am providing this answer, because when i searched for the answer, i couldn’t find much answer which explains same concept in the method syntax.
So In future, It may be useful for the people, those who intestinally searched method syntax like me today.
Thanks
karthik