1

I've been trying to access the JSON from this API. However, when I run a request to retrieve the JSON I'm met with the following list when I run this code.

const request = require('request');

var url = 'https://api.collegefootballdata.com/rankings?year=2019&week=10&seasonType=regular'
request(url,(err, res, body) => {
    if(!err && res.statusCode == 200) {
        var obj = JSON.parse(body);
        console.log(obj);
    }
});

My output:

[
  {
    season: 2019,
    seasonType: 'regular',
    week: 10,
    polls: [ [Object], [Object], [Object] ]
  }
]

My problem here lies in the fact that I want the information from each POLL (5 polls listed as objects in the JSON by following the link, however I cannot figure out how to WORK with this.) For example, I would want to call polls[0].rank to populate a list of the top 25 teams from the poll.

Accessing the arrays of objects inside nested JSON has obviously been a problem for me in Node, and would love to get started on the right foot when working with this project.

3

2 Answers 2

1

Looking at the JSON, there are 3 separate polls, and each poll has it's own top 25 list in ranks.

obj.polls[0].ranks
obj.polls[1].ranks
obj.polls[2].ranks

ranks is array of objects as well, so if you want to see the school the reference would be

obj.polls[0].ranks[0].school

You can easily examine the JSON, in a browser, using developer tools and Network tab.

enter image description here

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

3 Comments

When trying to call that example (console.log(obj.polls[0].ranks.school) I'm still getting " console.log(obj.polls[0].ranks[0].school); ^ TypeError: Cannot read property '0' of undefined"
Try obj[0] -- just need to get the root of the problem (pun intended)
Getting somewhere! The polls are no longer showing as 'Objects' in my array. => { season: 2019, seasonType: 'regular', week: 10, polls: [ { poll: 'Coaches Poll', ranks: [Array] }, { poll: 'AP Top 25', ranks: [Array] }, { poll: 'FCS Coaches Poll', ranks: [Array] } ] } When doing obj[0].polls[0] it loads! I guess I just wasn't aware I had to first index the whole object.
0

since polls is an array, I would love to use map so my code could be

obj.polls.map((pol)=>{ // so you can access the object pol which includes ranks and other proprites // do whatever you want here }

Comments

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.