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

I have a problem parsing some json message

Here is the response I get from the request, I outputted few because the file is large

{ "get": "fixtures", "parameters": { "live": "all" }, "errors": [], "results": 43, "paging": { "current": 1, "total": 1 }, "response": [ { "fixture": { "id": 1328845, "referee": null, "timezone": "UTC", "date": "2025-07-12T12:30:00+00:00", "timestamp": 1752323400, "periods": { "first": 1752323400, "second": null }, "venue": { "id": 21763, "name": "Skreia ipl kunstgress", "city": "Skreia" }, "status": { "long": "First Half", "short": "1H", "elapsed": 25, "extra": null } }, "league": { "id": 774, "name": "3. Division - Girone 1", "country": "Norway", "logo": "https://media.api-sports.io/football/leagues/774.png" +, "flag": "https://media.api-sports.io/flags/no.svg", "season": 2025, "round": "Group 1 - 14", "standings": true }, "teams": { "home": { "id": 23420, "name": "Sortland", "logo": "https://media.api-sports.io/football/teams/23420.pn +g", "winner": null }, "away": { "id": 6974, "name": "Bćrum", "logo": "https://media.api-sports.io/football/teams/6974.png +", "winner": null } }, "goals": { "home": 0, "away": 0 }, "score": { "halftime": { "home": 0, "away": 0 }, "fulltime": { "home": null, "away": null }, "extratime": { "home": null, "away": null }, "penalty": { "home": null, "away": null } }, "events": [] }, { "fixture": { "id": 1394987, "referee": null, "timezone": "UTC", "date": "2025-07-12T12:00:00+00:00", "timestamp": 1752321600, "periods": { "first": 1752321600, "second": null }, "venue": { "id": 21680, "name": "Estádio Deputado Galdino Leite", "city": "Salvador, Bahia" }, "status": { "long": "Halftime", "short": "HT", "elapsed": 45, "extra": null } }, "league": { "id": 1073, "name": "Baiano U20", "country": "Brazil", "logo": "https://media.api-sports.io/football/leagues/1073.png +", "flag": "https://media.api-sports.io/flags/br.svg", "season": 2025, "round": "Round of 16", "standings": false }, "teams": { "home": { "id": 25814, "name": "Galícia U20", "logo": "https://media.api-sports.io/football/teams/25814.pn +g", "winner": false }, "away": { "id": 13017, "name": "Jacuipense U20", "logo": "https://media.api-sports.io/football/teams/13017.pn +g", "winner": true } }, "goals": { "home": 1, "away": 2 }, "score": { "halftime": { "home": 1, "away": 2 }, "fulltime": { "home": null, "away": null }, "extratime": { "home": null, "away": null }, "penalty": { "home": null, "away": null } }, "events": [] } ] }

I removed use strict; because i was getting this error

Bareword "name" not allowed while "strict subs" in use at rss.pl line +26. Bareword "name" not allowed while "strict subs" in use at rss.pl line +27.

When i try to run my code this is the error i get Not an ARRAY reference at script.pl line 18.

my full script is

#!/usr/bin/perl use warnings; use LWP::UserAgent; use HTTP::Request; use JSON; my $url = "https://v3.football.api-sports.io/fixtures?live=all"; my $ua = LWP::UserAgent->new; my $req = HTTP::Request->new(GET => $url); $req->header('x-rapidapi-host' => 'v3.football.api-sports.io'); $req->header('x-rapidapi-key' => '.......'); my $response = $ua->request($req); my $parse_json = JSON::XS->new->decode ($response->content); my @matches = $parse_json->[1]; # Get all results in "response": [ ] if ($response->is_success) { foreach my $results (@matches) { my $matche_status = $results->{status}->{short}; my $teamA = $results->{teams}->{home}>{name}; my $teamB = $results->{teams}->{away}>{name}; my $teamA_scores_HT = $results->{score}->{halftime}->{home}; my $teamB_scores_HT = $results->{score}->{halftime}->{away}; my $teamA_scores_FT = $results->{score}->{fulltime}->{home}; my $teamB_scores_FT = $results->{score}->{fulltime}->{away}; print "$matche_status | $teamA has scored $teamA_scores_HT in +Halftime | $teamA_scores_FT in fulltime"; # Get all Home teams result +s print "$matche_status | $teamB has scored $teamB_scores_HT in +Halftime | $teamB_scores_FT in fulltime"; # Get all Away teams result +s # Print Such # HT | Arsenal has scored 1 in halftime | 2 in fulltime # FT | Chelsea has scored 2 in halftime | 3 in fulltime } } else { print $response->status_line; }

Replies are listed 'Best First'.
Re: parse json
by hippo (Archbishop) on Jul 12, 2025 at 17:11 UTC
    I removed use strict; because i was getting this error

    There were only two possible courses of action here: either fix the error or run without strict. You've chosen the wrong one. strict is there precisely to alert you to errors you have made so that you can fix them.


    🦛

Re: parse json
by marto (Cardinal) on Jul 12, 2025 at 15:15 UTC
    my $teamA = $results->{teams}->{home}>{name};

    missing hyphen in both instances of name?

    my $teamA = $results->{teams}->{home}->{name};

      Well, thanks i have seen that and the error for using strict nolonger appears again

      Just now having a problem of printing out results because am still getting this Not an ARRAY reference at script.pl line 19.

Re: parse json
by marto (Cardinal) on Jul 12, 2025 at 16:03 UTC

    Here's an example based on Mojo::JSON to get you started. Since I don't have access to the API you're using I've saved the example JSON to a file and slurped it in. You can use this as the guts of a program and simply chain the whole thing from a Mojo::UserAgent call to the API (e.g. $ua->get( .... )->result->json..), that way you don't need to worry about all the file reading business. Consider this an imperfect starting point:

    #!/usr/bin/perl use strict; use warnings; use feature 'say'; use Mojo::File qw(path); use Mojo::JSON qw(decode_json); # Path to your JSON file my $file = path('yay_sports.json'); # Slurp and decode JSON my $json = decode_json( $file->slurp ); # Loop through each match for my $match ( @{$json->{response}} ){ # match status my $status = $match->{fixture}{status}{long}; # team names my $home = $match->{teams}{home}{name}; my $away = $match->{teams}{away}{name}; # half time my $ht_home = $match->{score}{halftime}{home} // 'N/A'; my $ht_away = $match->{score}{halftime}{away} // 'N/A'; # full time my $ft_home = $match->{score}{fulltime}{home} // 'N/A'; my $ft_away = $match->{score}{fulltime}{away} // 'N/A'; # output, may want to consider not printing if things haven't # happend yet, where FT shows N/A for example say "Match: $home vs $away"; say "Status: $status"; say "Halftime Score: $home $ht_home - $ht_away $away"; say "Fulltime Score: $home $ft_home - $ft_away $away"; say "-" x 40; }

    Output:

    Match: Sortland vs B�rum Status: First Half Halftime Score: Sortland 0 - 0 B�rum Fulltime Score: Sortland N/A - N/A B�rum ---------------------------------------- Match: Gal�cia U20 vs Jacuipense U20 Status: Halftime Halftime Score: Gal�cia U20 1 - 2 Jacuipense U20 Fulltime Score: Gal�cia U20 N/A - N/A Jacuipense U20 ----------------------------------------

    There are some obvious issues I'm sure can see from the output, I'll leave them for you to address. Consider not jumping around from different sets of modules/frameworks if you don't have to. Mojolicious is very powerful, self contained and has everything you need to request and serve this content in a sane modern way.

      thank you sir, very much. tho i never used mojo, i have just corrected my mistakes and followed your example and made it work

Re: parse json
by eyepopslikeamosquito (Archbishop) on Jul 13, 2025 at 06:29 UTC
Re: parse json
by LanX (Saint) on Jul 12, 2025 at 15:23 UTC
    Strict was complaining about a "bareword name", did you try looking at the according lines (FWIW you changed the code before posting so it's lines 24 and 25)

    You might spot a typo right before {name}

    Saying this, we are not a code writing service.

    We are happy to help you learning Perl, but after your last thread I doubt you are remotely interested.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      "but after your last thread I doubt you are remotely interested."

      A little harsh and perhaps too early to tell IMHO :)

        We'll see! :)

        (FWIW his code is repeating a lot of issues I already listed... In vain)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery