1

I use Google plus API and get this data from it and get an error while parsing this JSON data

first part of json data that i parsing

enter image description here

and here is the code I use to parse this data and get error as data object is tested and work fine and hold data as it appear in past 2 images

var allIems = data.items;
for (var element in allIems) {
  document.getElementById('datafromapi').innerHTML +=
    (" , published time :" + element.published +
      " , last updated time :" + element.updated +
      ", complete url : " + element.url

    );
  var obj = element.object.attachments;

  document.getElementById('datafromapi').innerHTML +=
    (+"\nattachments of post :\n" +
      " type : " +
      obj[0].objectType +
      " ,displayName of content : " +
      obj[0].displayName +
      " ,content URL  : " +
      obj[0].url +
      " ,content data :" +
      obj[0].image.url +
      " ,content type : " +
      obj[0].image.type +
      " ,content height : " +
      obj[0].image.height +
      " ,content width : " +
      obj[0].image.width +
      "\n\n\n");


}
});

i got that error appear

Uncaught TypeError: Cannot read property 'attachments' of undefined

6
  • This error because NOT all element contain the object property. Commented Jun 23, 2017 at 18:18
  • the error is on attachments attribute not object and all elements have object attribute Commented Jun 23, 2017 at 18:21
  • @ahmedkhattab The error says that element.object is undefined. Since it's undefined, it doesn't have any attributes. Commented Jun 23, 2017 at 18:22
  • @ahmedkhattab he's right, the object attribute is undefined. Commented Jun 23, 2017 at 18:22
  • @ahmedkhattab Check your first item in your allIems array. Commented Jun 23, 2017 at 18:25

2 Answers 2

1

element values in

for (var element in allIems) {

are keys of allItems, which in this case are array indices. You have to address the actual array items like this:

var obj = allItems[element].object.attachments;

Your code element.object.attachments; tries to access property object of a number, which does not exist.

Since we know that allItems is an array, you could have written:

for (var i = 0; i < allIems.length; i++) {
  var obj = allItems[i].object.attachments;
Sign up to request clarification or add additional context in comments.

Comments

1

Javascript has a built in JSON parser you can use that takes in a string of the data and returns an object.

let jsonDataAsString = "{\"a\":1,\"b\":2}";

let jsonDataAsObject = JSON.parse(jsonDataAsString);

Then you can traverse through the data as an object, referencing properties using dot-notation

console.log(jsonDataAsObject.a); // 1

To be safe, you should be comparing properties to null before trying to use then

if(jsonDataAsObject.somePropery != null) {
    // the property exists so you can access it here
}

2 Comments

You likely want to check if the property is undefined first, as it could be defined and set to null (completely valid)
@scunliffe as far as I've ever used it null == undefined so as long as you us != you should be good. Though if you're someone who likes to use === then yes you would need to be more careful with checking before trying to access properties

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.