Here’s my subroutine:
my $json = JSON->new;
my $decoded_json = from_json( $response->decoded_content, { utf8 => 1 } );
print Dumper($decoded_json->{"response"}->{"recs"}->{"objs"}, length($decoded_json->{"response"}->{"recs"}->{"objs"})) . "\r\n";
foreach($decoded_json->{"response"}->{"recs"}->{"objs"}) {
# need to get name..
#print Dumper($_, length($_)) . "\r\n";
}
I’m grabbing JSON from a website (CloudFlare, actually) and trying to parse the data.
This is an example of the JSON. I am trying to get “name” from each of the results, and if “name” is what I want, I will get another field.
'response' => {
'recs' => {
'count' => 4,
'has_more' => bless( do{\(my $o = 0)}, 'JSON::XS::Boolean' ),
'objs' => [
{# <-- these
'ttl_ceil' => 86400,
'ttl' => '1',
'content' => '[SNIPPED]',
'zone_name' => 'xyz.info',
'display_content' => '[SNIPPED]',
'ssl_status' => undef,
'name' => 'xyz.info',
'prio' => undef,
'auto_ttl' => 1,
'display_name' => 'xyz.info',
'props' => {
'pending_ssl' => 0,
'cf_open' => 0,
'proxiable' => 1,
'expired_ssl' => 0,
'ssl' => 0,
'vanity_lock' => 0,
'expiring_ssl' => 0,
'cloud_on' => 1
},
'rec_tag' => '[SNIPPED]',
'rec_id' => '[SNIPPED]',
'type' => 'A',
'ssl_id' => undef,
'ssl_expires_on' => undef,
'service_mode' => '1'
},
{ # < -- these
'ttl_ceil' => 86400,
'ttl' => '1',
'content' => '0.0.0.0',
'zone_name' => 'xyz.info',
'display_content' => '0.0.0.0',
'ssl_status' => undef,
'name' => 'zzz.xyz.info',
'prio' => undef,
'auto_ttl' => 1,
'display_name' => 'zzz',
'props' => {
'pending_ssl' => 0,
'cf_open' => 0,
'proxiable' => 1,
'expired_ssl' => 0,
'ssl' => 0,
'vanity_lock' => 0,
'expiring_ssl' => 0,
'cloud_on' => 1
},
'rec_tag' => '[SNIPPED]',
'rec_id' => '[SNIPPED]',
'type' => 'A',
'ssl_id' => undef,
'ssl_expires_on' => undef,
'service_mode' => '1'
},
},
'result' => 'success'
};
You’re iterating over a reference (a single scalar), not over the contents of the referenced array: