3

I'm using the API's node wrapper: https://github.com/MySportsFeeds/mysportsfeeds-node/blob/master/README.md https://www.mysportsfeeds.com/data-feeds/api-docs#

The call is going through fine and automatically saving on under "/results"

Here's my code:

 msf.authenticate("username", "password");
    var data = msf.getData('nba', '2016-2017-regular', 'cumulative_player_stats', 'json', {
        player: 'nick-young'
    });

    request(data, function(error, response, body) {
        if (!error && response.statusCode == 200) {
            var parsedData = JSON.parse(body);
            console.log(parsedData["cumulativeplayerstats"]["playerstatsentry"][0]["stats"]["PtsPerGame"]["#text"]);
        }
    });

Thanks in advance

4
  • JSON.parse(obj) Commented Sep 18, 2017 at 6:02
  • "How to parse JSON in node js".... you are already doing it JSON.parse(body) if body is the actual JSON text response. If you are having a problem you should say what the actual problem is, and any error messages you might be receiving Commented Sep 18, 2017 at 6:02
  • have you console log error to make sure you arent getting an error? Did you console log just parseData to make sure it was parsed correctly? Did you console log body to make sure it is what you think it is? Commented Sep 18, 2017 at 6:07
  • I updated my question to the what is showing up in the console. JSON is saving in results folder. Do I need to get the JSON from the results folder then? Commented Sep 18, 2017 at 6:11

1 Answer 1

2

When you call msf.getData(league, season, feed, format, and any other applicable params for the feed) with format 'json'. It return a json object. As a result your data will be a json object.

msf.authenticate("username", "password");
var data = msf.getData('nba', '2016-2017-regular', 'cumulative_player_stats', 'json', {player: 'nick-young'});

console.log(data["cumulativeplayerstats"]["playerstatsentry"][0]["stats"]["PtsPerGame"]["#text"]);

Read json file content using fs.readFile

Sync

const fs = require('fs');
const json = JSON.parse(fs.readFileSync('results/cumulative_player_stats-nba-2016-2017-regular.json', 'utf8'));

Async

const fs = require('fs');

fs.readFile('results/cumulative_player_stats-nba-2016-2017-regular.json', 'utf8', (err, data) => {
  if (err) throw err;

  const json = JSON.parse(data);

  console.log(json["cumulativeplayerstats"]["playerstatsentry"][0]["stats"]["PtsPerGame"]["#text"]);
});
Sign up to request clarification or add additional context in comments.

2 Comments

The console says this with and without console.log : File saved as 'results/cumulative_player_stats-nba-2016-2017-regular.json'. How can I parse that JSON file?
Question on the async code. I'm getting an error on the const json; line. How can I get that resolved?

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.