Prakhar Gupta Prakhar Gupta - 2 months ago 8
Perl Question

Accessing elements in a list of json in perl

I have the following object

$VAR1 = [
{
'value' => {
'a' => 'd',
'b' => 'e',
'c' => 'f'
}
},
{
'value' => {
'a' => 'g',
'b' => 'h',
'c' => 'x'
}
}
];


I have to traverse this object to get values for a,b,c.
Please can someone help ?

Answer

Try this:

foreach my $arr (@$VAR1) {
    print $arr->{value}->@{ ('a','b','c') };
}

Or even shorter :

print map{ $_->{'value'}->@{ ('a','b','c') } } @{ $VAR1 }[0...@$VAR1 -1];

OUTPUT

defghx