Hi Monks!
I am trying to iterate through a JSON file. I need to extract all the data. In my example, it prints too many times cause it goes through the whole file, shouldn't it only print one time once it
finds it? Maybe there is a better and more efficient way to iterate over the file than how I am doing, any suggestions?
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
use JSON;
use JSON::XS;
use LWP::UserAgent;
use HTTP::Request;
my $ua = LWP::UserAgent->new();
my $req = HTTP::Request->new(GET => 'https://getmaydata/');
$req->header('User-Agent' => 'Mozilla/5.0');
$req->header( 'Content-Type' => 'application/json' );
$req->header('Accept' => '*/*');
my $response = $ua->request($req);
die $response->code if ! $response->is_success;
my $data = decode_json($response->decoded_content);
# This a sample json data I am getting back in $data
my $data = {
'zip' => 'AB3456',
'Tipo' => 'Boat',
'Province' => 'Open',
'aDate' => '2011-12-20T04:01:00.000Z',
'City' => 'South',
'State' => 'CO',
'eAddress' => '1001 Main ST',
'bDate' => '1999-01-02T11:43:48.199Z',
'xCode' => 'XPT CODE',
'Postal' => 'W23T6',
'happen' => [
{
'descri' => 'Yellow Bike Tube'
}
],
'explanation' => 'Order to go home.',
'dCity' => 'BYORDER',
'Statex' => 'NY',
'Add' => '222 - Life ST',
'reason' => 'Case broken',
'notas' => [
{
'corpus' => 'Free Ship:
CONTAC:
MORES:
TYPEZ
PLAN C',
'To' => 'XQ0000Q',
'ExtraName' => 'Test User',
'sub' => '44 Bokes broken'
}
],
'dados' => [
{
'valueA' => '2999',
'Creditd' => '-10',
'AddD' => '1 D ST',
'total' => '10',
'extra' => '-2000',
'city-z' => 'Delta',
'state-z' => 'MN',
'currency' => '220.00 usd',
'amount-e' => '30',
'zipE' => 'BG340',
'cets' => '9.0',
'nickname' => 'J R Junior Doe',
'extradata' => 'Donation',
'dadomv' => 'Open and reserved',
'getextra' => '230',
'netx' => '450',
'plus' => '-1000',
'total' => '340.00 usd',
'debt' => '30',
'collect' => '120.00 usd',
'color' => 'Blue'
}
],
'extraname' => 'Mary Lee Jones',
'account' => 'EX500RWX22'
};
for my $key ( keys %$data ) {
my $zip = $data->{'zip'};
my $descri = $data->{'happen'}[0]->{'descri'};
my $extra_name = $data->{'notas'}[0]->{'ExtraName'};
print "\n $zip - $descri - $extra_name\n";
}
Thanks for looking!!