Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Parsing data from JSON response file.

by Anonymous Monk
on Apr 11, 2020 at 02:08 UTC ( [id://11115357]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Parsing data from JSON response file.
by tangent (Parson) on Apr 11, 2020 at 04:02 UTC
    If the data is as you show in the example then you do not need the loop, you can just access whatever you need directly. To see what is happening within your loop, add another print statement:
    for my $key ( keys %$data ) { print "key: $key\n"; ...
      Good idea. That is actually one of the first things I did when running the code. And then came the realization that $key wasn't used anywhere within the loop!
      for my $key ( keys %$data ) { print "key = $key\n"; #debug my $zip = $data->{'zip'}; my $descri = $data->{'happen'}[0]->{'descri'}; my $extra_name = $data->{'notas'}[0]->{'ExtraName'}; print "\n $zip - $descri - $extra_name\n"; }
      I guess this is "off topic", but a feature of Perl.

      If you have a constant like: use constant DEBUG => 1; and then have a statement like print "whatever line(s)" if DEBUG;, Perl is smart enough to eliminate this statement from the executable code when DEBUG is false (set to zero). For complex modules, I often leave code like that in the source. When I add a new feature, I turn debugging on. Print is your friend. I seldom need the Perl debugger.

        Marshall:

        That's easy enough to verify with B::Concise:

        $ cat foo.pl use strict; use warnings; use constant DEBUG=>0; print "Foo bar!\n"; print "Baz\n" if DEBUG; $ perl -MO=Concise,-exec foo.pl 1 <0> enter 2 <;> nextstate(main 191 foo.pl:5) v:*,&,{,x*,x&,x$,$ 3 <0> pushmark s 4 <$> const[PV "Foo bar!\n"] s 5 <@> print vK 6 <;> nextstate(main 191 foo.pl:6) v:*,&,{,x*,x&,x$,$ 7 <@> leave[1 ref] vKP/REFC foo.pl syntax OK

        Yep, it's stripped out!

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Parsing data from JSON response file.
by Marshall (Canon) on Apr 11, 2020 at 03:23 UTC
    Your question appears to be about JSON rather than about LWP. Can you write a short script that I can run on my own computer?

    Update: I hacked out a lot, but not all extraneous stuff. Why do you need a loop here?

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; # This a sample json data I am getting back in $data my $data = { 'zip' => 'AB3456', 'Tipo' => 'Boat', 'happen' => [ { 'descri' => 'Yellow Bike Tube' } ], 'explanation' => 'Order to go home.', 'notas' => [ { 'corpus' => 'Free Ship:PLAN C', 'To' => 'XQ0000Q', 'ExtraName' => 'Test User', 'sub' => '44 Bokes broken' } ], 'dados' => [ { 'valueA' => '2999', 'Creditd' => '-10', 'collect' => '120.00 usd', 'color' => 'Blue' } ], 'extraname' => 'Mary Lee Jones', 'account' => 'EX500RWX22' }; # This is fine, why is loop needed? # If there where multiple records like this, then there # would be an additional level of indirection and a loop # would be needed to get record specific values of "zip", etc. my $zip = $data->{zip}; my $descri = $data->{happen}[0]->{descri}; my $extra_name = $data->{notas}[0]->{ExtraName}; print "\n $zip - $descri - $extra_name\n"; __END__ Prints: AB3456 - Yellow Bike Tube - Test User
Re: Parsing data from JSON response file.
by marto (Cardinal) on Apr 11, 2020 at 06:30 UTC

    If the example above contains the exact JSON response returned then you don't need a loop like others have said. I'd probably keep things simple and let Mojo::UserAgent do all the boring work for me.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11115357]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-26 05:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found