grammar My-Grammar { token TOP { \s* \s* } rule object { '{' \s* '}' \s* } rule pairlist { * % \, } rule pair { ':' } token string { \" <[ \w \s \- ' ]>+ \" } token number { [ \d+ [ \. \d+ ]? ] | [ \. \d+ ] } token value { | | | true | false | null } } #### my $source-string = q/{ "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "spouse": null }/; my $match = My-Grammar.parse($source-string); say ~$match if $match; # say $match.made if $match; #### { "firstName": "John", "lastName": "Smith", "isAlive": true, "age": 25, "address": { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "spouse": null } #### class My-actions { method TOP($/) { make $/.values.[0].made; }; method object ($/) { make $.made.hash.item; } method pairlist($/) { make $>>.made.flat; } method pair($/) { make $.made => $.made ; } method string($/) { make ~$/; } method number($/) { make +$/.Str; } method value($/) { given ~$/ { when "true" {make Bool::True;} when "false" {make Bool::False;} when "null" {make Any;} default { make ~$/;} } } } #### {"address" => { "streetAddress": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021-3100" }, "age" => 25, "firstName" => "John", "isAlive" => True, "lastName" => "Smith", "spouse" => (Any)} #### default { make ~$/;}