Access JSON::Path numbers only key - jsonpath

What is the correct syntax to acces a json key that does only has numbers with Perl6 Module JSON::Path? I'm getting "JSON path parse error at position 6" erros.
I would like to access items->2018->name:
use JSON::Path;
my Str $json = 「
{
"items" : {
"old" : { "name" : "olditem" },
"2017" : { "name" : "item1" },
"2018" : { "name" : "item2" },
"2019" : { "name" : "item3" }
}
}
」;
this is ok
#("olditem", "item3", "item1", "item2")
my JSON::Path $jp .= new: '.items[*].name';
say $jp.values($json);
is also ok
#("olditem")
$jp .= new: '.items.old.name';
say $jp.values($json);
does return nothing
#()
$jp .= new: ".items['2018'].name";
say $jp.values($json);
errors
#JSON path parse error at position 6
try {
$jp .= new: ".items.2018.name";
CATCH {
default { .Str.say }
}
}
errors also:
#JSON path parse error at position 6
try {
$jp .= new: ".items.['2018'].name";
CATCH {
default { .Str.say }
}
}
full error output:
#`[
JSON path parse error at position 6
in method giveup at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 43
in regex commandtree at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 15
in regex commandtree at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 15
in regex TOP at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 11
in submethod TWEAK at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 205
in method new at /tmp/.perl6/sources/B8E23055698DB40383876C0A68B2471D693FDC54 (JSON::Path) line 200
in block <unit> at test4 line 42
]
$jp .= new: ".items.['2018'].name";

The syntax attempted:
$jp .= new: ".items['2018'].name";
say $jp.values($json);
Was correct, however there was a bug in JSON::Path. It has been resolved in version 1.6 of the module.

Related

word boundary in regex with mongolite

I am facing a problem using word boundary regex with mongolite. It looks like the word boundary \b does not work, whereas it works in norm MongoDB queries.
Here is a working example:
I create this toy collection:
db.test2.insertMany([
{ item: "journal gouttiere"},
{ item: "notebook goutte"},
{ item: "paper plouf"},
{ item: "planner gouttement"},
{ item: "postcard goutte"}
]);
With mongosh:
db.test2.aggregate(
{
$match: {
item: RegExp("\\bgoutte\\b")
}
})
returns:
[
{
"_id": {
"$oid": "63206efeb0e1e89db6ef0c20"
},
"item": "notebook goutte"
},
{
"_id": {
"$oid": "63206efeb0e1e89db6ef0c23"
},
"item": "postcard goutte"
}
]
But:
library(mongolite)
connection <- mongo(collection="test2",db="test",
url = "mongodb://localhost:27017",
verbose = T)
connection$aggregate(pipeline = '[{
"$match": {
"item":{"$regex" : "\\bgoutte\\b", "$options" : "i"}
}
}]',options = '{"allowDiskUse":true}')
returns 0 lines. Changing to
connection$aggregate(pipeline = '[{
"$match": {
"item":{"$regex" : "goutte", "$options" : "i"}
}
}]',options = '{"allowDiskUse":true}')
Imported 3 records. Simplifying into dataframe...
_id item
1 63206efeb0e1e89db6ef0c20 notebook goutte
2 63206efeb0e1e89db6ef0c22 planner gouttement
3 63206efeb0e1e89db6ef0c23 postcard goutte
It looks like the word boundary regex does not work the same with mongolite. What is the proper solution ?
Ottie is right (and should post an answer!–I'd be fine with deleting mine then):
Backslashes have special meaning for both R and in the regex. You need two additional backslashes (one per \) to pass \\ from R to mongoDB (where you escape \b by \\b), see e.g. this SO question. I just checked:
con <- mongo(
"test",
url = "mongodb+srv://readwrite:test#cluster0-84vdt.mongodb.net/test"
)
con$insert('{"item": "notebook goutte" }')
con$insert('{"item": "postcard goutte" }')
Now
con$aggregate(pipeline = '[{
"$match": {
"item":{"$regex" : "\\\\bgoutte\\\\b", "$options" : "i"}
}
}]',options = '{"allowDiskUse":true}')
yields
_id item
1 63234ac1435f9b7c2a0787c2 notebook goutte
2 63234ac5435f9b7c2a0787c5 postcard goutte

syntax error, unexpected |=, expecting '}'

I want to build a json with jq filter, the usecase is very simple,
I have a json file as this,
# cat input.json
{"foo": 42, "bar": "less interesting data"}
and I want to generate a json file with jq as follow,
output:
increase the value of foo,
[
{
"leafCalls": {
"text": 43
}
}
]
and the filter I am using is defined as this,
# cat results.jq
[.[] | . as { foo: $leafText, bar: $leafCode} | {
leafCall: {
text: $leafText
},
} | {
leafCalls: .leafCall |= . + 1
}]
But there is a syntax issue as said below,
# <input.json jq --slurp --from-file results.jq > output.json
jq: error: syntax error, unexpected |=, expecting '}' (Unix shell quoting issues?) at <top-level>, line 6:
leafCalls: .leafCall |= . + 1
jq: 1 compile error
Beside, there is also a problem if want to concat a string to the value of "bar",
For what I want is,
[
{
"leafCalls": {
"text": "less interesting data string"
}
}
]
The filter is
# cat results2.jq
[.[] | . as { foo: $leafText, bar: $leafCode} | {
leafCall: {
text: $leafCode },
} | {
leafCalls: .leafCall + " " + "string"
}]
and the jq is saying,
jq: error: syntax error, unexpected '+', expecting '}'
Can anyone tell what's the problem with the filter definition?
To get the first output, increase the value directly. Parentheses are needed for precedence.
jq '[. as { foo: $leafText } | {
leafCalls: {
text: ($leafText + 1)
},
}
]' input.json
And similarly for the string:
jq '[ . as { foo: $leafText, bar: $leafCode}
| { leafCalls: { text: ($leafCode + " string" ) } } ]
' input.json

packer creating manifest.json is not valid json

I am using packer to create a base ami and using a post proccessor to create a manifest.json file
how can i make this json valid
{
"builds": [
{
"name": "amazon-ebs",
"builder_type": "amazon-ebs",
"build_time": 1589466697,
"files": null,
"artifact_id": "eu-west-1:ami-04d3331ac647e751b",
"packer_run_uuid": "add4c072-7ac2-f5e9-b941-6b80003c03ec",
"custom_data": {
"my_custom_data": "example"
}
}
],
"last_run_uuid": "add4c072-7ac2-f5e9-b941-6b80003c03ec"
2020-05-14T14:31:37.246153577Z stdout P }
Error: Parse error on line 13:
...b941-6b80003c03ec" 2020 - 05 - 14 T14:
----------------------^
Expecting 'EOF', '}', ':', ',', ']', got 'NUMBER'
My eventual goal is to save the artifact_id to a var using bash
Thank you for the help,
In order to make it valid json, i had to add this attribute to my packer template.json:
"post-processors": [
{
"type": "manifest",
"output": "manifest.json",
"strip_path": true,
"strip_time": true
"strip_time": "true"

Amavisd error mail_dispatch: no recognized protocol name

OS: FreeBSD-11.1
Name: amavisd-new-2.11.0_2,1
We recently began getting these errors from amavisd reported in our maillog:
. . .
proxy-reject: END-OF-MESSAGE: 451 4.5.0 Error in processing,
id=29937-07, quar+notif FAILED:
mail_dispatch: no recognized protocol name: -2
at /usr/local/sbin/amavisd line 9638.;
. . .
Each one of these errors results from processing messages from a single domain. However, not all of the traffic from that domain generates an error.
The code section in amavisd referred to in the message reads:
9619 my $any_deliveries = 0;
9620 my $per_recip_data = $msginfo->per_recip_data;
9621 my $num_recips_notdone =
9622 scalar(grep(!$_->recip_done && (!$filter || &$filter($_)),
9623 #$per_recip_data));
9624 while ($num_recips_notdone > 0) {
9625 # a delivery method may be a scalar of a form protocol:socket_specs, or
9626 # a listref of such elements; if a list is provided, it is expected that
9627 # each entry will be using the same protocol name, otherwise behaviour
9628 # is unspecified - so just obtain the protocol name from the first entry
9629 #
9630 my(%protocols, $any_tempfail);
9631 for my $r (#$per_recip_data) {
9632 if (!$dsn_per_recip_capable) {
9633 my $recip_smtp_response = $r->recip_smtp_response; # any 4xx code ?
9634 if (defined($recip_smtp_response) && $recip_smtp_response =~ /^4/) {
9635 $any_tempfail = $recip_smtp_response . ' (' . $r->recip_addr . ')';
9636 }
9637 }
9638 if (!$r->recip_done && (!$filter || &$filter($r))) {
9639 my $proto_sockname = $r->delivery_method;
9640 defined $proto_sockname
9641 or die "mail_dispatch: undefined delivery_method";
9642 !ref $proto_sockname || ref $proto_sockname eq 'ARRAY'
9643 or die "mail_dispatch: not a scalar or array ref: $proto_sockname";
9644 for (ref $proto_sockname ? #$proto_sockname : $proto_sockname) {
9645 local($1);
9646 if (/^([a-z][a-z0-9.+-]*):/si) { $protocols{lc($1)} = 1 }
9647 else { die "mail_dispatch: no recognized protocol name: $_" }
9648 }
9649 }
9650 }
But I have no idea where the protocol name sought is obtained. Because of the error the offending message is not placed in the quarantine folder so I cannot examine it.
Is this a configuration error on our part or is this the result of a malformed email transmission? In either case, what can I do to resolve this matter?

wordpress _e function

it shows"-- Debug: Undefined variable: wordscut on line 168 of /wp-content/theme"
function cutstr($string, $length) {
$string =strip_tags($string);
preg_match_all("/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf]|
[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf][x80-xbf]/", $string, $info);
for($i=0; $i<count($info[0]); $i++) {
$wordscut.= $info[0][$i];
$j= ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
if ($j > $length - 3) {
return $wordscut." ...";
}
}
return join('', $info[0]);
}
the above is my function. i know in php, it's right if a variable doesn't be declared before it is used.why it shows"Undefined variable: wordscut, j..... thank you.
2,* REQUIRED: Non-printable characters were found in the '''functions.php''' file. You may want to check this file for errors.
what is Non-printable characters .how to correct it? thank you.
This is one one classic bug.
When PHP started your script, $wordscut is not defined. When you run
$wordscut .= "sometext";
The code actually do
$wordscut = $wordscut . "sometext";
At this point, $wordscut is not available, thus Undefined Variable error occurred.
To fix it, add
$wordscut = '';
before
for($i=0; $i<count($info[0]); $i++) {

Resources