1

my first time trying to make an api request and get some data is not going to well.

I'm trying to find the "seed":"1" value and get the "franchise_id" value of "0010"

I haven't been successful even getting any of the seeds to console.log

Here is json

{
   "version":"1.0",
   "playoffBracket":{
      "bracket_id":"1",
      "playoffRound":[
         {
            "week":"14",
            "playoffGame":[
               {
                  "game_id":"1",
                  "away":{
                     "franchise_id":"0002",
                     "points":"137.2",
                     "seed":"3"
                  },
                  "home":{
                     "franchise_id":"0008",
                     "points":"111.7",
                     "seed":"6"
                  }
               },
               {
                  "game_id":"2",
                  "away":{
                     "franchise_id":"0006",
                     "points":"134.2",
                     "seed":"4"
                  },
                  "home":{
                     "franchise_id":"0011",
                     "points":"130.5",
                     "seed":"5"
                  }
               }
            ]
         },
         {
            "week":"15",
            "playoffGame":[
               {
                  "game_id":"3",
                  "away":{
                     "franchise_id":"0006",
                     "points":"153.3",
                     "winner_of_game":"2"
                  },
                  "home":{
                     "franchise_id":"0010",
                     "points":"162.8",
                     "seed":"1"
                  }
               },
               {
                  "game_id":"4",
                  "away":{
                     "franchise_id":"0002",
                     "points":"95.5",
                     "winner_of_game":"1"
                  },
                  "home":{
                     "franchise_id":"0012",
                     "points":"201.7",
                     "seed":"2"
                  }
               }
            ]
         }
      ]
   },
   "encoding":"utf-8"
}

i can log all the data , or some of the inner data , but haven't been able to do much else

$.ajax({
    url: "apiurlhere",
        success: function (data) {
            console.log(data);
            console.log(data.playoffBracket);
            console.log(data.playoffBracket[0]);
                }
});
clinton3141
  • 4,751
  • 3
  • 33
  • 46
MShack
  • 642
  • 1
  • 14
  • 33
  • Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – gre_gor Dec 18 '21 at 06:27

1 Answers1

1

That's because you are doing it wrong there is no playoffBracket[0] in your data. You need to do below:

data.playoffBracket.playoffRound[0]

To get franchise data you can use below:

data.playoffBracket.playoffRound[0].playoffGame[0].home

Or

data.playoffBracket.playoffRound[0].playoffGame[0].away

To get a single value

data.playoffBracket.playoffRound[0].playoffGame[0].home.franchise_id

Code to find the "seed":"1" value and get the "franchise_id" value of "0010"

// Method for searching
function findInJson(jsonData, findSeed, getFullObject = false) {
  let ret = null;
  for (let key in jsonData) {
    for (let key2 in jsonData[key]) {
      let awayHomeData = jsonData[key][key2];
      if (Array.isArray(awayHomeData)) {
        for (let key3 in awayHomeData) {
          if (
            awayHomeData[key3].hasOwnProperty("away") ||
            awayHomeData[key3].hasOwnProperty("home")
          ) {
            let homeOrAway = awayHomeData[key3];
            let homeSeed = homeOrAway.home.seed;
            let awaySeed = homeOrAway.away.seed;

            if (findSeed == awaySeed) {
              ret = homeOrAway.away;
            } else if (findSeed == homeSeed) {
              ret = homeOrAway.home;
            }
          }
        }
      }
    }
  }

  if (ret !== null) {
    ret = getFullObject ? ret : ret.franchise_id;
  }

  return ret;
}

// JSON Data
let data = {
  version: "1.0",
  playoffBracket: {
    bracket_id: "1",
    playoffRound: [
      {
        week: "14",
        playoffGame: [
          {
            game_id: "1",
            away: {
              franchise_id: "0002",
              points: "137.2",
              seed: "3",
            },
            home: {
              franchise_id: "0008",
              points: "111.7",
              seed: "6",
            },
          },
          {
            game_id: "2",
            away: {
              franchise_id: "0006",
              points: "134.2",
              seed: "4",
            },
            home: {
              franchise_id: "0011",
              points: "130.5",
              seed: "5",
            },
          },
        ],
      },
      {
        week: "15",
        playoffGame: [
          {
            game_id: "3",
            away: {
              franchise_id: "0006",
              points: "153.3",
              winner_of_game: "2",
            },
            home: {
              franchise_id: "0010",
              points: "162.8",
              seed: "1",
            },
          },
          {
            game_id: "4",
            away: {
              franchise_id: "0002",
              points: "95.5",
              winner_of_game: "1",
            },
            home: {
              franchise_id: "0012",
              points: "201.7",
              seed: "2",
            },
          },
        ],
      },
    ],
  },
  encoding: "utf-8",
};

// How to utilize the method
console.log(findInJson(data.playoffBracket.playoffRound, 22)); //will display null, because 22 doesn't exist
console.log(findInJson(data.playoffBracket.playoffRound, 2)); //will return 0012
console.log(findInJson(data.playoffBracket.playoffRound, 2, true)); //will return JSON object

The output looks as below:

null
"0012"
{
  franchise_id: "0012",
  points: "201.7",
  seed: "2"
}

The solution can be seen on JSFiddle as well.

Imran Zahoor
  • 2,521
  • 1
  • 28
  • 38
  • ok , so not knowing which one contains "seed":"1" value , how do i go about finding it , do i have to list every single item unil i find it ? – MShack Dec 18 '21 at 06:36
  • Definitely you'll have to iterate over data to find it, now you know how to get json data. – Imran Zahoor Dec 18 '21 at 06:39
  • ok so i just have to list infinetely because i have no idea how many playoffGame will be present , so list like so: data.playoffBracket.playoffRound[0].playoffGame[0].home.seed , data.playoffBracket.playoffRound[0].playoffGame[1].home.seed , data.playoffBracket.playoffRound[0].playoffGame[2].home.seed , data.playoffBracket.playoffRound[0].playoffGame[3].home.seed , data.playoffBracket.playoffRound[0].playoffGame[4].home.seed etc.....etc.... until i can match seed #1 – MShack Dec 18 '21 at 06:43
  • If you have the access to the backend, then filters should be implemented there. But if there aren't any, maybe you need to explore the API that you are using to fetch json. That should definitely support filters and even if that doesn't, either you loop over it indefinitely until you find required data. Or you place it in your own DB and get the required elements using query. – Imran Zahoor Dec 18 '21 at 06:46
  • ok more lost now then when i started , lol . Thanks – MShack Dec 18 '21 at 06:49
  • I think I already your question in detail, and you are now able to get the specific value. To iterate over the data you need to understand programming skills, especially loops. – Imran Zahoor Dec 18 '21 at 07:46
  • I'm trying to find the "seed":"1" value and get the "franchise_id" value of "0010" , i'm trying to get the answer for my question – MShack Dec 18 '21 at 07:49
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/240248/discussion-between-imran-zahoor-and-mshack). – Imran Zahoor Dec 18 '21 at 08:10
  • Done, you've the solution now. – Imran Zahoor Dec 18 '21 at 13:28