DanielSpaniel has asked for the wisdom of the Perl Monks concerning the following question:
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:
The JSON data, as output by Dumper, is below: (text edited for readability, so it fits nicely)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"; # } }
$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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Processing JSON with Perl
by huck (Prior) on Feb 17, 2017 at 20:53 UTC | |
by DanielSpaniel (Scribe) on Feb 17, 2017 at 20:57 UTC | |
by huck (Prior) on Feb 17, 2017 at 21:09 UTC | |
by DanielSpaniel (Scribe) on Feb 20, 2017 at 11:03 UTC | |
|
Re: Processing JSON with Perl
by Anonymous Monk on Feb 17, 2017 at 20:58 UTC | |
by DanielSpaniel (Scribe) on Feb 20, 2017 at 11:04 UTC |