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]);
                }
});
1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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 ?
Definitely you'll have to iterate over data to find it, now you know how to get json data.
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
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.
ok more lost now then when i started , lol . Thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.