JsonPath look for child element, but fallback to root element if child element is not found - jsonpath

How do I create json path syntax\query that tries and find a child element, but if it can't find it falls back to the root element?
I've tried the following:
['$.form_response.definition'],$
'$.form_response.definition',$`
'$.form_response.definition','$'
'$.form_response.definition',`this`
['$.form_response.definition']|$
... (and probably a lot more that I've already forgotten about)
but unfortunately nothing seems to work correctly or it just throws errors.
I would expect that it will try and look for the form_response.definition element and it that can't be found, it will just return the root element.
Let's for example take the following piece of code. In one JSON the part I'm looking for is in the under response.definition, but in the other JSON the part I'm looking for is directly at the root.
from jsonpath_ng import parse
parse('$.response.definition, $').find({'id': '123', 'response': {'id': '987', 'definition': {'title': 'hi'}}})
result = parse('$.response.definition, $').find({ 'id': '123', 'title': 'hi'})
But running this gives the following error.
raise JsonPathParserError('Parse error at %s:%s near token %s (%s)'
% (t.lineno, t.col, t.value, t.type))
jsonpath_ng.exceptions.JsonPathParserError: Parse error at 1:23 near token $ ($)
I've tried so many things by now. Is there a way to do this?

Related

How to display the error path of where the custom function is used?

If I use a built-in Sass function that returns an error, it will display the path of where it is used.
Using a built-in Sass function:
Code from _test.scss.
.foo {
color: darken(blue, s);
}
Will result in:
error _test.scss (Line 2: $amount: "s" is not a number for `darken')
Now, if I use a custom function that returns an error, it will display the path of where it is defined instead of where it is used.
Using a custom function:
Code from _test.scss.
.foo {
color: example(string);
}
Code from _functions.scss.
#function example($string) {
#error 'error message';
}
Will result in:
error core/utils/_functions.scss (Line 2: error message)
Is there any solution for solving this "issue"?
Your examples are similar only visually, underlying logic is completely different:
For built-in function Sass throws error by itself because code is not valid from Sass point of view.
Into second example you're throwing error because code is not valid from your point of view. Code itself if valid from Sass point of view in this case.
In both cases Sass displays same information about error location - a point into your codebase where error occurs. But in a case of your own, intentional error throwing actual point where error occurs actually matches a place where you're generating this error - exactly at a place where you have your #error directive. So there is no mistake into Sass behavior because it knows nothing about reasons why did you decide to throw an error.
You can always analyze stack trace that is displayed (at least by node-sass) along with error message to decide where did you get to point of error from. You can also use #debug to display context that may be useful for error analysis.

How to correctly combine "dot notation" and "braces notation" in cocoascript (sketch)?

In sketch documentation it's stated that dot and braces notations can be mixed with each other. It's even an example available:
[[context.document currentPage] deselectAllLayers];
Unfortunately, this code doesn't work in sketch and produce error if executed by "run custom script" command:
SyntaxError: Unexpected identifier 'currentPage'. Expected either a closing ']' or a ',' following an array element..
Plugin “untitled script”, line 2.
» [context.document currentPage]; «Error in command untitled script Script at path (null) does not contain a handler function named: onRun
Script executed in 0.023666s
This can be avoided by adding additional ( and ):
[[(context.document) currentPage] deselectAllLayers];
Why this happens? Is it any documentation available how exactly braces and dot notation can be mixed? Is it some error or expected behaviour?
It seems to me it's an error, but in Sketch documentation. Besides this case you showed, I couldn't find any other example where dot and braces notations are used together in the same statement, without parentheses.
The documentation page about Selections, for instance, tells that you'd use the following code to unselect everything:
var doc = context.document
[[doc currentPage] deselectAllLayers]
Follow this link and look under Clearing the selection header: http://bohemiancoding.com/sketch/support/developer/02-common-tasks/01.html
Even their example plugins don't mix both notations, as you can see here: https://github.com/BohemianCoding/ExampleSketchPlugins/blob/master/Hello%20World/Hello%20World.sketchplugin/Contents/Sketch/script.cocoascript.
In that example, context.document is also assigned to a new variable before being used within braces.

Unexpected token ILLEGAL in concatenated CSS file

I'm getting the following error in the Chrome console:
Uncaught SyntaxError: Unexpected token ILLEGAL
At the start of my concatenated CSS file:
#import url("http://fonts.googleapis.com/css?family=Raleway:300,400,600")
This line ends up on its own, and it's specifically this line that throws the error:
I've had a look around and it seems this error should relate to invisible characters making their way into the code. Unfortunately in my case that doesn't seem to be true. I've even deleted this portion of code and re-written it by hand to make sure - no difference.
I use Stylus (with gulp) and the resulting compile is naturally what throws the error - is this maybe an issue with gulp-stylus? I've looked at the compiled code and can't track down any invisible characters there either…
Does anything else throw this error?
The error implies you are trying to load the style sheet with <script> instead of <link rel=stylesheet>.
CSS is not JavaScript and can't be treated as such.

A simple AMZI prolog recursion, but is not giving the correct result as expected

So this is my .pro file content (AMZI PROLOG) :
room(kitchen).
room(office).
room(hall).
room('dining room').
room(cellar).
door(office, hall).
door(kitchen, office).
door(hall, 'dining room').
door(kitchen, cellar).
door('dining room', kitchen).
location(desk,office).
location(apple,kitchen).
location(flashlight,desk).
location('washing machine',cellar).
location(nani,'washing machine').
location(broccoli,kitchen).
location(crackers,kitchen).
location(computer,office).
location(envelope,desk).
location(stamp,envelope).
location(key,envelope).
edible(apple).
edible(crackers).
tastes_yucky(broccoli).
here(kitchen).
is_located_in(T1,T2):-location(X,T2),is_located_in(T1,X).
What I want to achieve is that if I use, for example, is_located_in(X,office), then the result will be :
X=desk,
X=flashlight,
X=computer,
X=envelope,
X=stamp,
X=key,
no
(in no particular order).
That way, the result will include things that basically located in / on the object in the office, instead of things that located directly in the office.
This is the source : http://www.amzi.com/AdventureInProlog/a8recurs.php
There is stated that the code are fine, but when I test it, it just returned :
no
Please help. Thanks.
Try this (in this order):
is_located_in(T1,T2):-location(T1,T2).
is_located_in(T1,T2):-location(X,T2),is_located_in(T1,X).

google Closure Compiler reports: JSC_TRAILING_COMMA

Im using google Closure Compiler to compress my js, anyhow I get the following error message:
JSC_TRAILING_COMMA: Parse error. Internet Explorer has a non-standard intepretation of trailing commas. Arrays will have the wrong length and objects will not parse at all. at line 8698 character 5 in post-login.js
];
The error is in the first line of this code, but I could not figure out whats wrong with it...
var plot = $.jqplot('usst_points_last_10_days', [data], {
title: '<h3 class="startGrafHeadline">' + global_language['discriptive']['usst']['visits_in_detail'] + '</h3>',
seriesColors: ["#00FF00"],
series: [{renderer:$.jqplot.BarRenderer}],
axesDefaults: {
tickRenderer: $.jqplot.CanvasAxisTickRenderer ,
tickOptions: {
angle: -30,
fontSize: '10pt'
}
},
axes: {
xaxis: {
renderer: $.jqplot.CategoryAxisRenderer
}
}
});
Try looking at the line just before the line that the compiler is complaining about. This will be the last line is some other file, perhaps.
If you examine your command line or build script, that will lead you to the answer. It seems likely that you are compiling multiple files or are pre-concatenating many files before compilation. What closure compiler is telling you is to look at line 8698.
If you have some logical explanation about why you think the error is contained in this code, please let us know your reasoning.
If you post more information, I may be able to improve this answer.
There is a handy tool for using closure-compiler through a web UI that may help you convince yourself that you have not found the offending line:
http://closure-compiler.appspot.com

Resources