Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access JSON::Path numbers only key

Tags:

jsonpath

raku

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";
like image 230
LuVa Avatar asked Apr 17 '19 17:04

LuVa


People also ask

What is JSON path expression?

JsonPath expressions always refer to a JSON structure in the same way as XPath expression are used in combination with an XML document. The "root member object" in JsonPath is always referred to as $ regardless if it is an object or array. JsonPath expressions can use the dot–notation. $.store.book[0].title.

What is JsonPath query?

JSONPath is a query language for JSON, similar to XPath for XML. It allows you to select and extract data from a JSON document. You use a JSONPath expression to traverse the path to an element in the JSON structure.

What does a JSON path look like?

A JsonPath expression begins with the dollar sign ( $ ) character, which refers to the root element of a query. The dollar sign is followed by a sequence of child elements, which are separated via dot (code) notation or via the square brackets (code).


1 Answers

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.

like image 139
Jonathan Worthington Avatar answered Sep 28 '22 09:09

Jonathan Worthington