Tom C Tom C - 3 months ago 11
JSON Question

JSON javascript display value undefined

I have a JSON object returned which looks like this

[
{
"competition": {
"name": "Premier League",
},
"nextState": 1,
"team_id": 1
}, {
"competition": {
"name": "Premier League",
},
"nextState": 1,
"team_id": 2
}
]


This a slimmed down version of the JSON, I am trying to access say the team_id

result = JSON.stringify(result, null, 4);
console.log(result);

$('#test').append(result);

alert(result[0].team_id);


All I seem to get is undefined, am I not access this correctly?

Many thanks

Answer

You have stringified your object result to a string. So for sure you can not access the attributes anymore.

Use a second variable for the stringified result:

var result_stringified = JSON.stringify(result, null, 4);
console.log(result_stringified);
$('#test').append(result_stringified);
alert(result[0].team_id);
Comments