3

I want to get name value. How do I access it?

json looks like this

enter image description here

    var result = null;
    xhr.addEventListener("readystatechange", function () {
      if (this.readyState === this.DONE) {

        result = JSON.parse(this.responseText);

           for(var i = 0; i <= result.total_count; i++)
           {
               console.log(result.data[i].name);
           }
      }
    });

This is giving me result as I want but with an error

Cannot read property 'name' of undefined in log.
0

2 Answers 2

7

You need to parse your JSON first:

const result = JSON.parse(this.responseText);
console.log(result.data[0].name);

The responseText property you use returns the response as purely text. In this case you are trying to read the data property of a String object. Since this doesn't exist it will be undefined. Next you are trying to access the 0 property of an undefined value which will result in the error you encounter.

Using the JSON.parse method will convert the string to a Javascript object which does have a data property.

Thanks @3limin4t0r for further clarifying in the comments!

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

8 Comments

this.responseText.data[0].name would try to access the data property of a string. String doesn't have a data property thus it returns undefined, accessing the property 0 of undefined will yield the error.
@3limin4t0r yes you are correct. It is not entirely correct to say a 'string cannot be used directly in Javascript'. I'll try and expand my answer.
@Mathyn My comment is nothing against your answer. I was just missing an explanation of why exactly this error occurred, so I thought I'd add it through the comments.
Still I think it better to explain why exactly this error occurred for future reference. Especially because strings are perfectly save to use in Javascript if used correctly.
Ahh! error is gone. I realized I did <= it shoud be <
|
-1

This is demo json

{
    "name": "mkyong",
    "age": 30,
    "address": {
        "streetAddress": "88 8nd Street",
        "city": "New York"
    },
    "phoneNumber": [
        {
            "type": "home",
            "number": "111 111-1111"
        },
        {
            "type": "fax",
            "number": "222 222-2222"
        }
    ]
}

follow this code :

<script>
       var data = '{"name": "mkyong","age": 30,"address": {"streetAddress": "88 8nd Street","city": "New York"},"phoneNumber": [{"type": "home","number": "111 111-1111"},{"type": "fax","number": "222 222-2222"}]}';

    var json = JSON.parse(data);

    alert(json["name"]); //mkyong
    alert(json.name); //mkyong

    alert(json.address.streetAddress); //88 8nd Street
    alert(json["address"].city); //New York

    alert(json.phoneNumber[0].number); //111 111-1111
    alert(json.phoneNumber[1].type); //fax

    alert(json.phoneNumber.number); //undefined
</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.