SkyWookie SkyWookie - 1 month ago 7
Javascript Question

JSON - how to get value in pure JS

I can't get any value from my JSON object.

I was trying:

console.log(players[0].player.info.position)


It return undefined.
What am i doing wrong?

Validator say that JSON in valid.
Here is JSON object:

{"players": [
{
"player": {
"info": {
"position": "aaa",
"shirtNum": 1,
"positionInfo": "aaa"
},
"nationalTeam": {
"isoCode": "aaa",
"country": "aaa",
"demonym": "aaa"
},
"age": "221",
"name": {
"first": "aaa",
"last": "aaa"
},
"id": 111,
"currentTeam": {
"name": "aaa",
"teamType": "aaa",
"shortName": "aaa",
"id": 21
}
}
}]}

Answer

You probably have that json in a variable which I call theThing. You need to use theThing to gain access to your json.

var theThing =    JSON.parse('{"players":[{"player":{"info":{"position":"aaa","shirtNum":1,"positionInfo":"aaa"},"nationalTeam":{"isoCode":"aaa","country":"aaa","demonym":"aaa"},"age":"221","name":{"first":"aaa","last":"aaa"},"id":111,"currentTeam":{"name":"aaa","teamType":"aaa","shortName":"aaa","id":21}}}]}');
alert(theThing.players[0].player.info.position);