JSONPath for matching child value - jsonpath

I've spent a lot of time trying to figure out how to write a json path which fits my needs, without success.
I hope someone will be able to help me.
I've got a very simple JSON object that looks like: {"code":..., "message":...}, note that this is not a list of objects
I'm trying to find a JSON path that returns [%code%] when code > 100, else [].
I've found $[?(#.code > 100)].code which works fine with Jayway & Gatling implementations, but doesn't with Nebahle and Goessner implementations (according to http://jsonpath.herokuapp.com/).
Sadly the project I'm working on is using a jsonpath lib with Goessner implementation.
Does anyone have an idea of a jsonpath that could work for any implementation?
Thanks!

There is no agreement between different JSONPath implementations for applying a filter expression to a JSON object. Some apply it to the object itself (e.g. Jayway), some apply it to each value of the JSON object (e.g. Goessner Javascript), and some don't seem to support it at all (e.g. Goessner PHP). Christoph Burgmer's JSONPath comparisons reports these differences here.
In your case, assuming Goessner Javascript, you might be able to get away with
$[?(#>100)] (1)
or perhaps safer
$[?(typeof(#) == 'number' && # > 100)] (2)
For example, given {"code":101, "message":"101"}, (1) produces
[101,"101"]
while (2) produces
[101]
(according to http://jsonpath.herokuapp.com/).

Related

Gremlin math step has issues if a reserved word "exp" is used in a label name

I have a query which has the math function like below,
math('number1-expected_value').next()
It throws error as
**GremlinServerError: 499: {"detailedMessage":"Unknown function or variable 'cted_value' at pos 20 in expression 'number1 - expected_value'","requestId":"01e3f9e6-3cf2-4af0-bf94-5a4979d488b4","code":"InvalidParameterException"}**
I know, the exp keyword is reserved for exponentiation operation. Is there anyone who knows how to use a keyword as a normal string inside the math function in gremlin?
Note:
when I change the variable as "eexpected_value", it works. If I choose expeected_value, it throws same error. Hence expe and expee make operation of exponen
This appears to be an issue in the Gremlin math step which is built using EXP4J. I have opened the following Jira issue to track within the Apache TinkerPop project: https://issues.apache.org/jira/browse/TINKERPOP-2856
For now, the best workaround as you discovered is just to avoid using variable names that contain any of the math built in function names (like exp).

glob()-like C function however supporting the "**"-recursive syntax?

I've just noticed that passing e.g.: **/main.c to POSIX function glob() doesn't yield correct results (they are empty). Is there a similar and also such a common (POSIX, etc.) function that would support the "**"-recursive syntax? Or maybe some single file, small library with such feature?

Access the if statement via base::if() in R

I am coding in R and due to stability purposes when I have to deploy something, I call every function with the syntax package::function(arguments) just to avoid conflicts that as you know may happen when using a lot of packages. It helped me a lot over the years.
I know that if is a reserved word so technically speaking it is impossible (or at least it should be in my knowledge) for someone to define an object and name it if.
I am also aware that it belongs to control flow statement (which I think are a different "thing") and due to the previous consideration I am also aware that the following questions might be useless. My pure technical doubts are:
Why if I embrace it in back-ticks the function class returns "function" as a result?
Why without back-ticks I get an error? and last but most important
Why I am unable to access it via the usual base::if() syntax?
As I said, most likely useless questions but at this point I am curious about the details underneath it.
> class(if)
Error: unexpected ')' in "class(if)"
> class(`if`)
[1] "function"
> base::if(T) T
Error: unexpected 'if' in "base::if"
> if(T) T
[1] TRUE
> base::if(`T`) T
Error: unexpected 'if' in "base::if"
if-with-backticks actually returns .Primitive("if")
The R language definition section on "Internal vs Primitive" specifies that .Primitive objects include
“Special functions” which really are language elements, but implemented as primitive functions:
{ ( if for while repeat break next
return function quote switch
The reason that a naked "if" without backticks or base::if don't work is that the "language elements" above are treated as special cases by R's parser. Once you have typed base::, R's parser expects the next symbol to be a regular symbol that can be looked up in the base namespace. base::if, base::for, and base::( all return errors because R does not expect these special elements to occur at this position in the input stream; they are syntactically incorrect.

What is the difference between ?matrix and ?matrix()

I was going through swirl() again as a refresher, and I've noticed that the author of swirl says the command ?matrix is the correct form to calling for a help screen. But, when I run ?matrix(), it still works? Is there a difference between having and not having a pair of parenthesis?
It's not specific to the swirl environment (about which I was entirely unaware until 5 minutes ago) That is standard for R. The help page for the ? shortcut says:
Arguments
topic
Usually, a name or character string specifying the topic for which help is sought.
Alternatively, a function call to ask for documentation on a corresponding S4 method: see the section on S4 method documentation. The calls pkg::topic and pkg:::topic are treated specially, and look for help on topic in package pkg.
It something like the second option that is being invoked with the command:
?matrix()
Since ?? is actually a different shortcut one needs to use this code to bring up that page, just as one needs to use quoted strings for help with for, if, next or any of the other reserved words in R:
?'?' # See ?Reserved
This is not based on a "fuzzy logic" search in hte help system. Using help instead of ? gets a different response:
> help("str()")
No documentation for ‘str()’ in specified packages and libraries:
you could try ‘??str()’
You can see the full code for the ? function by typing ? at the command line, but I am just showing how it starts the language level processing of the expressions given to it:
`?`
function (e1, e2)
{
if (missing(e2)) {
type <- NULL
topicExpr <- substitute(e1)
}
#further output omitted
By running matrix and in general any_function you get the source code of it.

Finding ? operator in AST

I'm doing some exercise in Rascal. When I try to determine the Cyclomatic complexity of a Java method getting methods from an AST. I would like to evaluate the ? operator.
As it is not determined by '/if(_, _, )', I tried to determine it using postfix(, _); (infix works fine finding || or &&)
Still no success.
Anybody who can unhide this secret to me?
Thanks in Advance
Use the force; the source for the AST definition is here https://github.com/cwi-swat/rascal/blob/master/src/org/rascalmpl/library/lang/java/m3/AST.rsc, also mentioning the conditional constructor Mark pointed out in his comment.
If you use iprintln on the AST for a small example, like iprintln(ast) or import util::ValueUI; and then text(ast) for an editor with the formatted AST it's easy to find out what ASTs look like.

Resources