Forum Discussion
the “in” operator return true if there a property with null value. and if you further check for sub-child it errors out.
example 1:-
$:{
child:null
}
example 2:-
$: {
child: {
child1: “some value”
}
$.hasOwnProperty() helps in filtering this type of use cause while “in” operator return true for example-1, (‘child’ in $) and will generate an error “Requires an Array or Object in-the-Right-hand-side” for example-1 if you try this expression: (‘child’ in $) && (‘child1’ in $.child) but it will work for example 2.
get() method works for both the case in order to filter out property based on null try this: ($.get(“child”) != null) && ($.child.get(“child1”) != null). Is “in” operator (shorthand for get()) bugged ?
Get method:
Sorry for bad formatting.
- tstack9 years agoFormer Employee
Yes, the “in” operator only tests to see if the property is in the object and not what it’s value might be. If the “child” property can be null in your data, you would have to check for that as well, by doing something like the following:
('child' in $) && $child && ('value' in $child)
Which is, admittedly, ugly. However, I’m not really sure how common this would really be, so I imagine most expressions would not have to do such a thing.
Still, this calls for some solution and I think we would prefer to modify the jsonPath() function to support default values instead of changing the hasOwnProperty() in a way that deviates from the standard.
The failure comes from “(‘child1’ in $.child)” since “$.child” is null, which matches the JavaScript behavior.
- nganapathiraju9 years agoFormer Employee
Your expression should be
'child' in $ && 'child1' in $child
The above will return true is child1 exists in child. Its not $.child
Hope that makes sense.
- jaybodra9 years agoNew Contributor III
isn’t this expression $.child compliant with java script?
- tstack9 years agoFormer Employee
Yes, “$.child”, “$child”, and “$[‘child’]” all mean the same thing in the expression language, which is to get the value of the property named “child”. The “$” symbol refers to the input document, so “$.child” and “$[‘child’]” mean lookup “child” in the document. The “$child” syntax (i.e. no dot) is a shorthand that is accepted.