The easiest way is to enumerate all paths and then only return those that match your criteria:

#!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);

I've omitted reading in the JSON file into a Perl structure. Use JSON::Tiny or Mojo::JSON or CPANEL::JSON::XS for that.


In reply to Re: Obtain the full path of a json key by Corion
in thread Obtain the full path of a json key by eny

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.