0

Here is my JSON:

[
  {
    "0": "324",
    "1": "Cavill ",
    "2": "11",
    "3": "100018463",
    "4": "RAR",
    "5": "DummyX",
    "6": "DummyY",
    "7": "Moretext",
    "8": "moretext",
    "id": "lol",
    "teacher": "Specsavers ",
    "rate": "11",
    "teacherid": "100018463",
    "address": "114 Road X",
    "postcode": "WXER 21",
    "lat": "51.511871",
    "lon": "-0.112934",
    "distance": "0.023308985382378217"
  }
]

This is held in a variable called "hold".

I am trying to output "teacher" in a div called output1.

Here is my code:

obj = JSON.parse(hold);
document.getElementById("output1").innerHTML = obj[1].teacher;

I keep getting undefined.

I tried changing 1 to 0. Same issue - undefined.

What am I doing wrong here?

How do I access attribute data from each node/branch?

7
  • Your JSON is inside an Array, so use obj = JSON.parse(hold[0]); Commented Aug 18, 2016 at 10:25
  • 2
    do you have really a JSON string? Commented Aug 18, 2016 at 10:26
  • @Pugazh What?! No. hold would (should) be a string. Commented Aug 18, 2016 at 10:26
  • Are you sure you need to parse it? try doing hold[0].teacher Commented Aug 18, 2016 at 10:28
  • 2
    can you reproduce the problem in stacksnippet ? Commented Aug 18, 2016 at 10:28

3 Answers 3

2

Index starts from 0 and in your data index 1 is undefined.

obj = JSON.parse(hold);
document.getElementById("output1").innerHTML = obj[0].teacher;
//------------------------------------------------^^^------
Sign up to request clarification or add additional context in comments.

Comments

1

Make sure hold is actually a JSON string. And the way you are accessing the parsed json is wrong, it should be 0 since there's only one json string.

var hold = '[ { "0": "324","1": "Cavill ","2": "11", "3": "100018463", "4": "RAR", "5": "DummyX","6": "DummyY","7": "Moretext",  "8": "moretext","id": "lol","teacher": "Specsavers ", "rate": "11","teacherid": "100018463", "address": "114 Road X", "postcode": "WXER 21","lat": "51.511871","lon": "-0.112934", "distance": "0.023308985382378217" }]';
var obj = JSON.parse(hold);
document.getElementById('output1').innerHTML = obj[0].teacher;
<div id="output1"></div>

Comments

0

Array index starts from 0. So in your parsed json, obj[0] represents the first element. As there is no second element obj[1] will be undefined.

Use obj[0] instead obj[1].

Working Code:

var hold = '[{  "0": "324",  "1": "Cavill ",  "2": "11",  "3": "100018463",  "4": "RAR",  "5": "DummyX",  "6": "DummyY",  "7": "Moretext",  "8": "moretext",  "id": "lol",  "teacher": "Specsavers ",  "rate": "11",  "teacherid": "100018463",  "address": "114 Road X",  "postcode": "WXER 21",  "lat": "51.511871",  "lon": "-0.112934",  "distance": "0.023308985382378217"}]';

var obj = JSON.parse(hold);
document.getElementById("output1").innerHTML = obj[0].teacher;
<div id="output1"></div>

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.