Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: parse json

by Corion (Patriarch)
on Sep 14, 2025 at 17:53 UTC ( [id://11166222]=note: print w/replies, xml ) Need Help??


in reply to parse json

Please explain to us where you are having problems and where the code you have deviates from your expectations.

If your script does not print what you expect, consider that ->{competitions} and ->{competitors} are arrays.

Putting the use strict; pragma at the top of your code allows Perl to tell you where you are going wrong.

Replies are listed 'Best First'.
Re^2: parse json
by frank1 (Monk) on Sep 14, 2025 at 19:15 UTC

    this is my code below, am getting the results very well, my problem is parsing them, i tried this -> coz of an array but still cant parse out the message

    #!/usr/bin/perl use strict; use warnings; use LWP::UserAgent; use HTTP::Request; use JSON; my $url = 'link'; my $ua = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 }, ); $ua->agent("MyApp/0.1"); my $req = HTTP::Request->new(GET => $url); my $response = $ua->request($req); my $parse_json = JSON::XS->new->decode ($response->content); if ($response->is_success) { for my $match (@{$parse_json->{leagues}}) { my $elapsed = $match->{events}->{competitions}->{status}{display +Clock}; my $status = $match->{events}->{competitions}->{wasSuspended}; my $home = $match->{events}->{competitors}->{team}; my $away = $match->{events}->{competitors}->{team}; my $away_goal = $match->{events}->{competitions}->{competitors}-> +{score}; my $home_goal = $match->{events}->{competitions}->{competitors}-> +{score}; print "$elapsed: $home: $home_goal: Suspended Match: $status"; print "$elapsed: $away: $away_goal: Suspended Match: $status"; } } else { print $response->decoded_content; print $response->status_line, "n"; }

      You must be getting some kind of error message. What is the error message?

      Also, compare, manually, your code to your data structure:

      my $elapsed = $match->{events}->{competitions}->{status}{displayCloc +k} # HASH HASH HASH HASH

      Your data structure has array elements, for example events is an array.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11166222]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2025-11-14 17:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your view on AI coding assistants?





    Results (71 votes). Check out past polls.

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.