Home > Mobile >  Need to get data from fixture JSON
Need to get data from fixture JSON

Time:07-16

I am struggling hard since yesterday getting the data from JSON, I don't understand why the fixture is not accessible. When I tried to get the ID, it gives me an error saying id isn't defined.

This gives me an error saying ID is not defined:

res.send(data.response["fixture"].id)

This code returns nothing:

res.send(data.response.fixture);

This code gives me the JSON data:

res.send(data.response

My JSON data:

// 20220714200426
// http://localhost:9090/

[
  {
    "fixture": {
      "id": 822924,
      "referee": "Jhon Alexander Hinestroza Romana, Colombia",
      "timezone": "UTC",
      "date": "2022-07-15T01:10:00 00:00",
      "timestamp": 1657847400,
      "periods": {
        "first": 1657847400,
        "second": 1657851000
      },
      "venue": {
        "id": 375,
        "name": "Estadio Manuel Murillo Toro",
        "city": "Ibagué"
      },
      "status": {
        "long": "Second Half",
        "short": "2H",
        "elapsed": 90
      }
    },
    "league": {
      "id": 239,
      "name": "Primera A",
      "country": "Colombia",
      "logo": "https://media.api-sports.io/football/leagues/239.png",
      "flag": "https://media.api-sports.io/flags/co.svg",
      "season": 2022,
      "round": "Clausura - 2"
    },
    "teams": {
      "home": {
        "id": 1142,
        "name": "Deportes Tolima",
        "logo": "https://media.api-sports.io/football/teams/1142.png",
        "winner": true
      },
      "away": {
        "id": 1127,
        "name": "Deportivo Cali",
        "logo": "https://media.api-sports.io/football/teams/1127.png",
        "winner": false
      }
    },
    "goals": {
      "home": 2,
      "away": 0
    },
    "score": {
      "halftime": {
        "home": 0,
        "away": 0
      },
      "fulltime": {
        "home": null,
        "away": null
      },
      "extratime": {
        "home": null,
        "away": null
      },
      "penalty": {
        "home": null,
        "away": null
      }
    },
    "events": [
      {
        "time": {
          "elapsed": 1,
          "extra": null
        },
        "team": {
          "id": 1127,
          "name": "Deportivo Cali",
          "logo": "https://media.api-sports.io/football/teams/1127.png"
        },
        "player": {
          "id": 13398,
          "name": "Y. Congo"
        },
        "assist": {
          "id": null,
          "name": null
        },
        "type": "Card",
        "detail": "Yellow Card",
        "comments": null
      },
      {
        "time": {
          "elapsed": 26,
          "extra": null
        },
        "team": {
          "id": 1127,
          "name": "Deportivo Cali",
          "logo": "https://media.api-sports.io/football/teams/1127.png"
        },
        "player": {
          "id": 13592,
          "name": "T. Gutierrez"
        },
        "assist": {
          "id": null,
          "name": null
        },
        "type": "Card",
        "detail": "Yellow Card",
        "comments": null
      },
      {
        "time": {
          "elapsed": 27,
          "extra": null
        },
        "team": {
          "id": 1142,
          "name": "Deportes Tolima",
          "logo": "https://media.api-sports.io/football/teams/1142.png"
        },
        "player": {
          "id": 13304,
          "name": "B. Rovira"
        },
        "assist": {
          "id": null,
          "name": null
        },
        "type": "Card",
        "detail": "Yellow Card",
        "comments": null
      },
      {
        "time": {
          "elapsed": 40,
          "extra": null
        },
        "team": {
          "id": 1127,
          "name": "Deportivo Cali",
          "logo": "https://media.api-sports.io/football/teams/1127.png"
        },
        "player": {
          "id": 13592,
          "name": "T. Gutierrez"
        },
        "assist": {
          "id": null,
          "name": null
        },
        "type": "Card",
        "detail": "Red Card",
        "comments": null
      },
      {
        "time": {
          "elapsed": 56,
          "extra": null
        },
        "team": {
          "id": 1127,
          "name": "Deportivo Cali",
          "logo": "https://media.api-sports.io/football/teams/1127.png"
        },
        "player": {
          "id": null,
          "name": "O. Acosta"
        },
        "assist": {
          "id": null,
          "name": null
        },
        "type": "Card",
        "detail": "Yellow Card",
        "comments": null
      },
      {
        "time": {
          "elapsed": 59,
          "extra": null
        },
        "team": {
          "id": 1142,
          "name": "Deportes Tolima",
          "logo": "https://media.api-sports.io/football/teams/1142.png"
        },
        "player": {
          "id": 283830,
          "name": "L. Riascos"
        },
        "assist": {
          "id": null,
          "name": null
        },
        "type": "Card",
        "detail": "Yellow Card",
        "comments": null
      },
      {
        "time": {
          "elapsed": 61,
          "extra": null
        },
        "team": {
          "id": 1142,
          "name": "Deportes Tolima",
          "logo": "https://media.api-sports.io/football/teams/1142.png"
        },
        "player": {
          "id": 13348,
          "name": "L. Miranda"
        },
        "assist": {
          "id": null,
          "name": null
        },
        "type": "Var",
        "detail": "Goal Disallowed - offside",
        "comments": null
      },
      {
        "time": {
          "elapsed": 64,
          "extra": null
        },
        "team": {
          "id": 1127,
          "name": "Deportivo Cali",
          "logo": "https://media.api-sports.io/football/teams/1127.png"
        },
        "player": {
          "id": 10328,
          "name": "Y. Gonzalez"
        },
        "assist": {
          "id": null,
          "name": "O. Acosta"
        },
        "type": "subst",
        "detail": "Substitution 1",
        "comments": null
      },
      {
        "time": {
          "elapsed": 66,
          "extra": null
        },
        "team": {
          "id": 1142,
          "name": "Deportes Tolima",
          "logo": "https://media.api-sports.io/football/teams/1142.png"
        },
        "player": {
          "id": 6632,
          "name": "A. Renteria"
        },
        "assist": {
          "id": 13348,
          "name": "L. Miranda"
        },
        "type": "subst",
        "detail": "Substitution 1",
        "comments": null
      },
      {
        "time": {
          "elapsed": 69,
          "extra": null
        },
        "team": {
          "id": 1142,
          "name": "Deportes Tolima",
          "logo": "https://media.api-sports.io/football/teams/1142.png"
        },
        "player": {
          "id": 13596,
          "name": "M. Rangel"
        },
        "assist": {
          "id": 13298,
          "name": "J. Lucumi"
        },
        "type": "Goal",
        "detail": "Normal Goal",
        "comments": null
      },
      {
        "time": {
          "elapsed": 70,
          "extra": null
        },
        "team": {
          "id": 1127,
          "name": "Deportivo Cali",
          "logo": "https://media.api-sports.io/football/teams/1127.png"
        },
        "player": {
          "id": 59887,
          "name": "A. Gutierrez"
        },
        "assist": {
          "id": null,
          "name": null
        },
        "type": "Card",
        "detail": "Yellow Card",
        "comments": null
      },
      {
        "time": {
          "elapsed": 72,
          "extra": null
        },
        "team": {
          "id": 1142,
          "name": "Deportes Tolima",
          "logo": "https://media.api-sports.io/football/teams/1142.png"
        },
        "player": {
          "id": 13298,
          "name": "J. Lucumi"
        },
        "assist": {
          "id": 6632,
          "name": "A. Renteria"
        },
        "type": "Goal",
        "detail": "Normal Goal",
        "comments": null
      },
      {
        "time": {
          "elapsed": 78,
          "extra": null
        },
        "team": {
          "id": 1127,
          "name": "Deportivo Cali",
          "logo": "https://media.api-sports.io/football/teams/1127.png"
        },
        "player": {
          "id": 31691,
          "name": "A. Vuletich"
        },
        "assist": {
          "id": 51135,
          "name": "H. Mosquera"
        },
        "type": "subst",
        "detail": "Substitution 2",
        "comments": null
      },
      {
        "time": {
          "elapsed": 78,
          "extra": null
        },
        "team": {
          "id": 1127,
          "name": "Deportivo Cali",
          "logo": "https://media.api-sports.io/football/teams/1127.png"
        },
        "player": {
          "id": 200150,
          "name": "J. C. Caldera Alvis"
        },
        "assist": {
          "id": 51165,
          "name": "A. Rodriguez"
        },
        "type": "subst",
        "detail": "Substitution 3",
        "comments": null
      },
      {
        "time": {
          "elapsed": 82,
          "extra": null
        },
        "team": {
          "id": 1127,
          "name": "Deportivo Cali",
          "logo": "https://media.api-sports.io/football/teams/1127.png"
        },
        "player": {
          "id": 200150,
          "name": "J. C. Caldera Alvis"
        },
        "assist": {
          "id": null,
          "name": null
        },
        "type": "Card",
        "detail": "Yellow Card",
        "comments": null
      },
      {
        "time": {
          "elapsed": 84,
          "extra": null
        },
        "team": {
          "id": 1142,
          "name": "Deportes Tolima",
          "logo": "https://media.api-sports.io/football/teams/1142.png"
        },
        "player": {
          "id": 59758,
          "name": "A. Melendez"
        },
        "assist": {
          "id": 35757,
          "name": "Y. Orozco"
        },
        "type": "subst",
        "detail": "Substitution 2",
        "comments": null
      }
    ]
  },
  {
    "fixture": {
      "id": 896998,
      "referee": null,
      "timezone": "UTC",
      "date": "2022-07-15T02:00:00 00:00",
      "timestamp": 1657850400,
      "periods": {
        "first": 1657850400,
        "second": 1657854000
      },
      "venue": {
        "id": 1087,
        "name": "Estadio Universitario de Nuevo León",
        "city": "San Nicolás de los Garza"
      },
      "status": {
        "long": "Second Half",
        "short": "2H",
        "elapsed": 46
      }
    },
    "league": {
      "id": 927,
      "name": "World Cup - Women - Qualification Concacaf",
      "country": "World",
      "logo": "https://media.api-sports.io/football/leagues/927.png",
      "flag": null,
      "season": 2022,
      "round": "Semi-finals"
    },
    "teams": {
      "home": {
        "id": 1717,
        "name": "Canada W",
        "logo": "https://media.api-sports.io/football/teams/1717.png",
        "winner": true
      },
      "away": {
        "id": 1785,
        "name": "Jamaica W",
        "logo": "https://media.api-sports.io/football/teams/1785.png",
        "winner": false
      }
    },
    "goals": {
      "home": 1,
      "away": 0
    },
    "score": {
      "halftime": {
        "home": 1,
        "away": 0
      },
      "fulltime": {
        "home": null,
        "away": null
      },
      "extratime": {
        "home": null,
        "away": null
      },
      "penalty": {
        "home": null,
        "away": null
      }
    },
    "events": [
      {
        "time": {
          "elapsed": 10,
          "extra": null
        },
        "team": {
          "id": 1785,
          "name": "Jamaica W",
          "logo": "https://media.api-sports.io/football/teams/1785.png"
        },
        "player": {
          "id": null,
          "name": "C. Swaby"
        },
        "assist": {
          "id": null,
          "name": "J. C. Pelaia-Hylton"
        },
        "type": "subst",
        "detail": "Substitution 1",
        "comments": null
      },
      {
        "time": {
          "elapsed": 18,
          "extra": null
        },
        "team": {
          "id": 1717,
          "name": "Canada W",
          "logo": "https://media.api-sports.io/football/teams/1717.png"
        },
        "player": {
          "id": null,
          "name": "J. Fleming"
        },
        "assist": {
          "id": null,
          "name": null
        },
        "type": "Goal",
        "detail": "Normal Goal",
        "comments": null
      }
    ]
  },
  {
    "fixture": {
      "id": 868553,
      "referee": null,
      "timezone": "UTC",
      "date": "2022-07-15T02:05:00 00:00",
      "timestamp": 1657850700,
      "periods": {
        "first": 1657850700,
        "second": null
      },
      "venue": {
        "id": 1844,
        "name": "Estadio Olímpico Carlos Iturralde Rivero",
        "city": "Mérida"
      },
      "status": {
        "long": "Halftime",
        "short": "HT",
        "elapsed": 45
      }
    },
    "league": {
      "id": 263,
      "name": "Ascenso MX",
      "country": "Mexico",
      "logo": "https://media.api-sports.io/football/leagues/263.png",
      "flag": "https://media.api-sports.io/flags/mx.svg",
      "season": 2022,
      "round": "Apertura - 3"
    },
    "teams": {
      "home": {
        "id": 2311,
        "name": "Venados FC",
        "logo": "https://media.api-sports.io/football/teams/2311.png",
        "winner": true
      },
      "away": {
        "id": 2308,
        "name": "Celaya",
        "logo": "https://media.api-sports.io/football/teams/2308.png",
        "winner": false
      }
    },
    "goals": {
      "home": 2,
      "away": 1
    },
    "score": {
      "halftime": {
        "home": 2,
        "away": 1
      },
      "fulltime": {
        "home": null,
        "away": null
      },
      "extratime": {
        "home": null,
        "away": null
      },
      "penalty": {
        "home": null,
        "away": null
      }
    },
    "events": [
      {
        "time": {
          "elapsed": 11,
          "extra": null
        },
        "team": {
          "id": 2308,
          "name": "Celaya",
          "logo": "https://media.api-sports.io/football/teams/2308.png"
        },
        "player": {
          "id": null,
          "name": "D. Gonzalez"
        },
        "assist": {
          "id": null,
          "name": null
        },
        "type": "Goal",
        "detail": "Normal Goal",
        "comments": null
      },
      {
        "time": {
          "elapsed": 17,
          "extra": null
        },
        "team": {
          "id": 2311,
          "name": "Venados FC",
          "logo": "https://media.api-sports.io/football/teams/2311.png"
        },
        "player": {
          "id": null,
          "name": "M. Perez"
        },
        "assist": {
          "id": null,
          "name": null
        },
        "type": "Goal",
        "detail": "Normal Goal",
        "comments": null
      },
      {
        "time": {
          "elapsed": 36,
          "extra": null
        },
        "team": {
          "id": 2311,
          "name": "Venados FC",
          "logo": "https://media.api-sports.io/football/teams/2311.png"
        },
        "player": {
          "id": 36091,
          "name": "M. Perez"
        },
        "assist": {
          "id": null,
          "name": null
        },
        "type": "Goal",
        "detail": "Normal Goal",
        "comments": null
      }
    ]
  }
]

My Node.js code:

const express = require('express');
const Router = express.Router();//router will  be used now to route
require('dotenv').config();
const fetch = require('cross-fetch');
const axios = require('axios');
const request = require('request');




const endpoint = 'fixtures';
    
  
Router.get('/', (req, res) => {
  var options = {
    method: 'GET',
    url: `https://v3.football.api-sports.io/${endpoint}`,
    qs: {live: 'all'},
    headers: {
      'x-rapidapi-host': 'v3.football.api-sports.io',
      'x-rapidapi-key': `${process.env.API_KEY}`
    }
  };
  
  request(options, function (error, resp, body) {
    if (error) throw new Error(error);
    const data = JSON.parse(body);
    res.send(data.response);
  });
  
    
})

module.exports = Router;


What I want is to access fixture ID and all other keys as well but I am not able to reach the fixture. Can anyone able to guide me on what's wrong I am doing?

CodePudding user response:

From the json data response body above, data.response is an array of objects of multiple fixtures. So, data.response.fixture would always return undefined. To access a particular fixture, depending on what you want to do to with, you'd have to access the index of the response first. As such:

console.log(data.response[0].fixture)

this should return:

    "fixture": {
      "id": 822924,
      "referee": "Jhon Alexander Hinestroza Romana, Colombia",
      "timezone": "UTC",
      "date": "2022-07-15T01:10:00 00:00",
      "timestamp": 1657847400,
      "periods": {
        "first": 1657847400,
        "second": 1657851000
      },
      "venue": {
        "id": 375,
        "name": "Estadio Manuel Murillo Toro",
        "city": "Ibagué"
      },
      "status": {
        "long": "Second Half",
        "short": "2H",
        "elapsed": 90
      }
    }

To access its id:

console.log(data.response[0].fixture.id)

The important thing is to access the properties through the array index of the element you want.

  • Related