8

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";
LuVa
  • 2,288
  • 2
  • 10
  • 20
  • 3
    As far as I can tell this is a bug in JSON::Path: [This line](https://modules.perl6.org/dist/JSON::Path:cpan:JNTHN/lib/JSON/Path.pm6#L27) shouldn't be there. – melpomene Apr 17 '19 at 18:16
  • 2
    thx, I did open an [issue](https://github.com/jnthn/json-path/issues/10) – LuVa Apr 17 '19 at 18:39
  • 3
    `.items['2018'].name` would have been the right syntax. The bug is that it doesn't return anything. – melpomene Apr 17 '19 at 18:41

1 Answers1

9

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.

Jonathan Worthington
  • 29,104
  • 2
  • 97
  • 136