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.