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!!


In reply to Parsing data from JSON response file. by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.