#!perl use 5.020; use feature 'signatures', 'postderef'; no warnings 'experimental::signatures'; sub all_json_paths( $obj, $prefix = '' ) { #say "$prefix|$obj"; if( !ref $obj ) { return $prefix } elsif( ref $obj eq 'ARRAY' ) { my @res; my $idx = 1; # 1-based or 0-based indexing? for my $el ($obj->@*) { push @res, all_json_paths( $el, $prefix . "[$idx]" ); $idx++; } return @res } elsif( ref $obj eq 'HASH' ) { my @res; for my $k (sort keys $obj->%*) { push @res, all_json_paths( $obj->{$k}, $prefix . ".$k" ); } return @res } } sub path_to_key( $key, $obj ) { my @p = all_json_paths( $obj ); return grep /\Q$key\E$/, all_json_paths( $obj ) } my $struct = [ { "Obj1"=> [ { "Obj11Id"=> "Id11", "Obj11Version"=> "v1", "Obj11Attributes"=> { "Obj11AttributesObj1"=> { "Obj11AttributesObj1Key1"=> "1", "Obj11AttributesObj1Key2"=> "3" }, "Obj11AttributesObj2"=> { "Obj11AttributesObj2Key1"=> "9" } } }, { "Obj12Id"=> "Id12", "Obj12Version"=> "v1", "Obj12Attributes"=> { "Obj12AttributesObj1"=> { "Obj12AttributesObj1Key1"=> "2", "Obj12AttributesObj1Key2"=> "4" }, "Obj12AttributesObj2"=> { "Obj12AttributesObj2Key1"=> "8" }, "Obj12AttributesArray1"=> [ { "Obj12AttributesArray1Obj1Key1"=> 1 } ] } } ] }, { "Obj2"=> [ { "Obj21Id"=> "Id21", "Obj21Version"=> "v1", "Obj21Attributes"=> { "Obj21AttributesObj1"=> { "Obj21AttributesObj1Key1"=> "3", "Obj21AttributesObj1Key2"=> "5" }, "Obj21AttributesObj2"=> { "Obj21AttributesObj2Key1"=> "7" } } }, { "Obj22Id"=> "Id22", "Obj22Version"=> "v1", "Obj22Attributes"=> { "Obj22AttributesObj1"=> { "Obj22AttributesObj1Key1"=> "4", "Obj22AttributesObj1Key2"=> "6" }, "Obj22AttributesObj2"=> { "Obj22AttributesObj2Key1"=> "0" }, "Obj22AttributesArray1"=> [ { "Obj22AttributesArray1Obj1Key1"=> 1 } ] } } ] } ]; say path_to_key('Obj21Id', $struct);