Home > database >  get specific value from json array inside of array
get specific value from json array inside of array

Time:12-18

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

CodePudding user response:

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.

  • Related