loop over JSON using Perl

json, perl

Solution

Use JSON or JSON::XS to decode the JSON into a Perl structure.

Simple example:

use strict;
use warnings;

use JSON::XS;

my $json = '[{"Year":"2012","Quarter":"Q3","DataType":"Other 3","Environment":"STEVE","Amount":125},{"Year":"2012","Quarter":"Q4","DataType":"Other 2","Environment":"MIKE","Amount":500}]';

my $arrayref = decode_json $json;

foreach my $item( @$arrayref ) { 
    # fields are in $item->{Year}, $item->{Quarter}, etc.
}

Problem

I'm a newbie to Perl and want to loop over this JSON data and just print it out to the screen. How can I do that? ``` $arr = '[{"Year":"2012","Quarter":"Q3","DataType":"Other 3","Environment":"STEVE","Amount":125},{"Year":"2012","Quarter":"Q4","DataType":"Other 2","Environment":"MIKE","Amount":500}]'; ```

Original source

Related problems