Sergio Gutiérrez
Sergio Gutiérrez

Reputation: 68

JSON on Node js

i'm new to Node, and i want to parse a JSON from an API.

The thing is that i can access to almost all of the JSON, but it has some elements i'm not getting.

var request = require("request");

var url = 'https://cf.biwenger.com/api/v1/competitions/la-liga/data';
request(url, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        var jsonObject = JSON.parse(body);

        console.log(jsonObject.data.teams);
    }
});

I have this code to obtain the teams on the JSON, and that is what i obtain:

{ '1': 
  { id: 1,
    name: 'Athletic',
    slug: 'athletic',
    nextMatch: 
     { id: 4665,
       date: 1522505700,
       round: [Object],
       home: true,
       against: [Object] } },
 '2': 
  { id: 2,
    name: 'Atlético',
    slug: 'atletico',
    nextMatch: 
     { id: 4668,
       date: 1522608300,
       round: [Object],
       home: true,
       against: [Object] } },
 '3': 
  { id: 3,
    name: 'Barcelona',
    slug: 'barcelona',
    nextMatch: 
     { id: 4666,
       date: 1522521900,
       round: [Object],
       home: false,
       against: [Object] } },
 ...(and much more)

Now i want to obtain all the names of the teams, i tried to do it, but i always got undefined, unless i put something like:

console.log(jsonObject.data.teams["1"]["name"]); //Athletic
console.log(jsonObject.data.teams["2"]["name"]); //Atlético

How can i iterate through that JSON without getting undefined??

Thank you so much!!

Upvotes: 2

Views: 162

Answers (3)

Jack
Jack

Reputation: 804

Get an array by using Object.values and then map over it

const data = { 
  '1': { 
    id: 1,
    name: 'Athletic',
    slug: 'athletic'
  },
  '2': {
    id: 2,
    name: 'Man Utd',
    slug: 'man-utd'
  },
  '3': {
    id: 3,
    name: 'Liverpool',
    slug: 'liverpool'
  }
};

const getTeams = data => 
  Object.values(data)
    .map(team => team.name);
    
console.log(getTeams(data));

You can read about Object.values here, and the Array.prototype.map function here

Upvotes: 1

Evan Williams
Evan Williams

Reputation: 154

In order to iterate through the names of each team in the most straightforward sense, you can iterate over each team, then access the name of each team:

for (var id in jsonObject.teams) {
  if (jsonObject.teams.hasOwnProperty(id)) {
    console.log(jsonObject.teams[id].name);
  }
}

I am not sure what you mean when you say you're "getting undefined," though. Could you please show us the way you're attempting to do it so we can understand the error in your understanding?

Upvotes: 0

Ele
Ele

Reputation: 33726

Get the values using the function Object.values and loop over that array using a for-loop:

Object.values(jsonObject.data.teams).forEach(function(team) {
    console.log(team.name)
})

var jsonObject = {  data: {    teams: {      '1': {        id: 1,        name: 'Athletic',        slug: 'athletic',        nextMatch: {          id: 4665,          date: 1522505700,          round: [Object],          home: true,          against: [Object]        }      },      '2': {        id: 2,        name: 'Atlético',        slug: 'atletico',        nextMatch: {          id: 4668,          date: 1522608300,          round: [Object],          home: true,          against: [Object]        }      },      '3': {        id: 3,        name: 'Barcelona',        slug: 'barcelona',        nextMatch: {          id: 4666,          date: 1522521900,          round: [Object],          home: false,          against: [Object]        }      }    }  }}

Object.values(jsonObject.data.teams).forEach(function(team) {
    console.log(team.name);
});
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 0

Related Questions