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 | |
|
Re: parse json
by marto (Cardinal) on Jul 12, 2025 at 15:15 UTC | |
by joyfedl (Acolyte) on Jul 12, 2025 at 15:40 UTC | |
by LanX (Saint) on Jul 12, 2025 at 16:13 UTC | |
|
Re: parse json
by marto (Cardinal) on Jul 12, 2025 at 16:03 UTC | |
by joyfedl (Acolyte) on Jul 12, 2025 at 17:30 UTC | |
|
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 | |
by marto (Cardinal) on Jul 12, 2025 at 16:05 UTC | |
by LanX (Saint) on Jul 12, 2025 at 16:15 UTC |