You are quick

But what if the json is fluid, and $data->{sections}[3] is not always directions, but you want to do the directions? Just search for directions and keep its reference!

use strict; use warnings; my $data = { 'sections' => [ { 'images' => [], 'level' => 1, 'content' => [], 'title' => '10-minute Turkey' }, { 'images' => [], 'level' => 2, 'content' => [ { 'text' => 'Makes 4 servings.', 'type' => 'paragraph' } ], 'title' => 'Description' }, { 'images' => [], 'level' => 2, 'content' => [ { 'elements' => [ { 'elements' => [], 'text' => 'abc' }, { 'elements' => [], 'text' => 'efg' }, { 'elements' => [], 'text' => 'hij' }, ], 'type' => 'list' } ], 'title' => 'Ingredients' }, { 'images' => [], 'level' => 2, 'content' => [ { 'elements' => [ { 'elements' => [], 'text' => 'tuv' }, { 'elements' => [], 'text' => 'wxy' }, { 'elements' => [], 'text' => 'z12' }, ], 'type' => 'list' } ], 'title' => 'Directions' } ] }; #deepdump($data,'$data->'); my $directions; for my $i (@{$data->{sections}}) { if ($i->{title} eq 'Directions') {$directions=$i} } if ($directions) { deepdump($directions,'$directions->'); } sub deepdump { my $data=shift; my $isname=shift; my $iam=ref $data; if ($iam eq 'HASH') { my @keys=keys(%$data); if (0==scalar(@keys)){print $isname.'={}'."\n";} else {for my $k (@keys) { deepdump($data->{$k},$isname.'{'.$k.'}'); +} } } # hash elsif ($iam eq 'ARRAY') { my $ict=0; if (0==scalar(@$data)){print $isname.'=[]'."\n";} else {for my $i (@$data) { deepdump($data->[$ict],$isname.'['.$ict.' +]'); $ict++} } } # array else {print $isname.'='.$data."\n";} } exit;
Result:
$directions->{title}=Directions $directions->{level}=2 $directions->{content}[0]{type}=list $directions->{content}[0]{elements}[0]{elements}=[] $directions->{content}[0]{elements}[0]{text}=tuv $directions->{content}[0]{elements}[1]{elements}=[] $directions->{content}[0]{elements}[1]{text}=wxy $directions->{content}[0]{elements}[2]{text}=z12 $directions->{content}[0]{elements}[2]{elements}=[] $directions->{images}=[]
I think this second hint may also prove usefull!


In reply to Re^3: Processing JSON with Perl by huck
in thread Processing JSON with Perl by DanielSpaniel

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.