Hi,

I seem to be having troubles and misunderstandings with processing JSON. My difficulty is likely in large part due to the fact that I don't really seem to understand the variable structures properly.

So, I have a JSON file, and I've a script which is using the JSON module, and I've decoded the data, etc, and now have a little loop where I want to work my way through the file and print it out. However, although I can print a few bits from the top level of the structure, I'm having problems trying to access anything else. FYI the JSON file is data retrieved from one of the wikimedia sites, but it seems valid.

The relevant Perl code is immediately below, while the JSON file data, as output by Dumper is below that:

my $data=decode_json($rawdata); # print Dumper($decoded); for ( @{ $data->{sections} } ) { # This line works; prints the 4 titles (one from each section) print $_->{title}."\n"; # No image info' in file anyway, so no output expected, but not sure i +f code is correct for ( @{ $_->{images} } ) { print $_."\n"; } # Nothing gets printed, but no error (figure it should print something + from top section) print $_{content}->{text}."\n"; # This fails with "Not a hash reference" # for ( @{ $_->{content}->{elements} } ) # { # print $_->{text}."\n"; # } }
The JSON data, as output by Dumper, is below: (text edited for readability, so it fits nicely)
$VAR1 = { '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' } ] };

The actual URL I'm retrieving, for testing, is: http://recipes.wikia.com/api/v1/Articles/AsSimpleJson?id=24372

So, I don't really seem to get how to access anything which is at a lower level, such as {sections}->{content}->{elements}->{text}, or {sections}->{content}->{text}. I'm also a bit confused as to how to distinguish, programatically, between the two bottom sections (i.e. "Directions" and "Ingredients"); for instance, how, if I only wanted "text" fields from the latter, could I distinguish it, in code, from the former (i.e. the third section, titled "Ingredients"?

Any assistance would be most welcome.

Thanks!

In reply to 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.