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.