Louis Dufour
Louis Dufour

Reputation: 13

Parse complex JSON files "undefined"

I got "undefined" while trying to Parse this JSON file:

 {
"responses": [
    {
      "labelAnnotations": [
        {
          "mid": "/m/01yrx",
          "description": "cat",
          "score": 0.9926739,
          "topicality": 0.9926739
        },
        {
          "mid": "/m/01l7qd",
          "description": "whiskers",
          "score": 0.9639658,
          "topicality": 0.9639658
        },
        {
          "mid": "/m/083jv",
          "description": "white",
          "score": 0.9582038,
          "topicality": 0.9582038
        },
        {
          "mid": "/m/0k0pj",
          "description": "nose",
          "score": 0.9425352,
          "topicality": 0.9425352
        },
        {
          "mid": "/m/06z04",
          "description": "skin",
          "score": 0.92025506,
          "topicality": 0.92025506
        }
      ]
    }
  ]
}

This file is th result from a XMLHttpRequest from the google vision API and This is what i'm doing to print "description" field:

e.onload=function(){
  var i= JSON.parse(e.response);
  value = i.responses[0]["description"];
  alert(value);
};

Upvotes: 1

Views: 73

Answers (1)

Geenzo
Geenzo

Reputation: 86

"undefined" is coming from the below statement

value = i.responses[0]["description"];

as labelAnnotations is an object within an array you will reach description using

value = i.responses[0].labelAnnotations[0]["description"];

Upvotes: 2

Related Questions