-2

So just a little context first, I'm writing a small JS app that displays sports information. Basically I am working with an NBA api that has all teams and all players. What I'd like to do is periodically call the AllTeams and AllPlayers endpoints, receive the data, then store the data in a local JSON file.

At the moment the data from the AllTeams endpoint looks like:

team data

And the player data looks like:

enter image description here

However in my local json file I'd like each team to have a key of "players" and then store the players from the AllPlayers endpoint according to team.id

Just to clarify I don't have a problem with doing the api calls etc the bit that I'm not sure how to do is set a structure for my json file and save the actual data from the two endpoints in that json file.

The structure I'd like is something like this:

enter image description here

Thanks, Bez

Behzad Lashkari
  • 177
  • 1
  • 9

2 Answers2

1

Let's say you already have this data from api (and already parsed it so you only have the array data)

// data from api
const allTeams = [
    {
      id: 1,
      abbreviation: 'ATL',
      ... // other fields
    },
    {
      id: 2,
      abbreviation: 'BOS',
      ... // other fields
    },
]

// data from api
const players = [
  {
     id: 1,
     name: 'Buggy Sama',
     team: {
       id: 1,
       abbreviation: 'ATL'
       // other team fields
     }
     // other player fields
  }
]
  • To form the data you want, what you can do is insert every players inside the team via map:
const jsonPerTeam = allTeams.map(team => {
  const teamPlayers= {}

  // you can use any other loop functions here if you prefer that, 
  // but map is the fastest and just a personal preference
  players.map(player => {

    // if player's team is the same as the team, then store that player on player's key under teamPlayers
    if (player.team.id === team.id) {
      teamPlayers[player.id] = player;
    }
  })

   // add teamPlayers as players on your team
   return {
      ...team,
      players: teamPlayers
   }
});

^^ the code above will generate the structure that you want (based on the post).

  • To store the data you can check this answer

so you can do something like:

import fs from 'fs'

fs.writeFile("myLocalJsonTeam.json", jsonPerTeam , function(err) {
    if (err) {
        console.error(err);
    }
});

Kindly clarify if I understood your question correctly

I am L
  • 4,288
  • 6
  • 32
  • 49
0

First of all, it seems like the allPlayers is paganized only returning 25 players at a time, I don't know if that amount is alterable but if it isn't make sure you loop through each page and push all players to a local variable.

var fs = require('fs');

let allPlayers = []; // Contains list of all players
const allTeams = []; // Contains list of all teams

const getAllTeamsWithPlayers = allTeams.map( team => {
  // Get a list of all players that play for the team
  const allPlayersForTeam = allPlayers.filter((player, index) => {
    // Check if this player plays for the team
    const playsForTeam = player.team.id == team.id;
    // If so, delete this player from the list (that way in next iteration it doesn't have to be looped over again)
    if (playsForTeam) allPlayers.splice(index, 1);
    return playsForTeam;
  });
 team.players = allPlayersForTeam;
 return team;
});

// Convert object to json
const allTeamsWithPlayersJSON = JSON.stringify(getAllTeamsWithPlayers());

fs.writeFile('myjsonfile.json', allTeamsWithPlayersJSON, 'utf8', console.log);
jasper
  • 937
  • 1
  • 9
  • 22