0

I'm unable to fetch JSON value to check if the person is wearing glasses while taking photo, the value relies in four arrays: photos, tags, attributes and glasses, I want to check the value "value" is true or false. I use alert() to test if value fetched but nothing comes out. I don't know which part gets wrong

I use JavaScript to fetch the JSON value as follows:

var facesDet = $.getJSON( APIdetect, function() {
  console.log( "success" );
  console.log(facesDet);
})

.done(function (facesDet, tid) {
    var VALUEIWANT = facesDet.photos[0].tags[0].attributes[0].gender[0].value;

});

The JSON value looks as follows:

{
"photos" : [
    {
        "url" : "http://tinyurl.com/673cksr",
        "pid" : "F@0c95576847e9cd7123f1e304476b59ae_59ec9bb2ad15f",
        "width" : 375,
        "height" : 409,
        "tags" : [
            {
                "tid" : "TEMP_F@0c95576847e9cd7123f1e304b1dcbe53_59ec9bb2ad15f_56.53_40.83_0_1",
                "recognizable" : true,
                "confirmed" : false,
                "manual" : false,
                "width" : 30.67,
                "height" : 28.12,
                "center" : { "x" : 56.53, "y" : 40.83},
                "eye_left" : { "x" : 66.93, "y" : 33.99},
                "eye_right" : { "x" : 51.73, "y" : 33.99},
                "yaw" : -16,
                "roll" : 0,
                "pitch" : 0,
                "attributes" : {
                    "face" : { "value" : "true", "confidence" : 82 },
                    "gender" : { "value" : "female", "confidence" : 80 },
                    "glasses":{"value" : "true", "confidence" : 100},
                    "dark_glasses":{"value" : "true", "confidence" : 72},
                    "smiling":{"value" : "false", "confidence" : 35}
                }
            }
        ]
    }
],
"status" : "success",
"usage" : {
    "used" : 1,
    "remaining" : 99,
    "limit" : 100,
    "reset_time_text" : "Fri, 21 September 2012 12:57:19 +0000",
    "reset_time" : 1348232239
}
}
3
  • 3
    use this 'facesDet=JSON.parse(facesDet)' and then use this ' var VALUEIWANT=facesDet.photos[0].tags[0].attributes.gender.value; ' Commented Jan 11, 2016 at 9:56
  • 2
    attributes and gender are Objects not Arrays so you would get the properties like so. facesDet.photos[0].tags[0].attributes.gender.value or using the array bracket syntax facesDet.photos[0].tags[0]['attributes']['gender']['value'] Commented Jan 11, 2016 at 9:57
  • Thank you for spotting my error. Commented Jan 11, 2016 at 10:04

4 Answers 4

2

You tryed to take value from gender like from array:

var VALUEIWANT = facesDet.photos[0].tags[0].attributes[0].gender[0].value;

But in your json its object, so you should use:

var VALUEIWANT = facesDet.photos[0].tags[0].attributes.gender.value;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for spotting my problem, since I saw curly bracket, so I thought it's an array, I should realise only values encompassed with [] is array.
2

Your access is wrong. You have an object, but you treat it like an array

var VALUEIWANT = facesDet.photos[0].tags[0].attributes[0].gender[0].value;
//                                                    ^^^  and  ^^^

right access:

var VALUEIWANT = facesDet.photos[0].tags[0].attributes.gender.value;

data:

"attributes" : {
    "face" : { "value" : "true", "confidence" : 82 },
    "gender" : { "value" : "female", "confidence" : 80 },
    "glasses":{"value" : "true", "confidence" : 100},
    "dark_glasses":{"value" : "true", "confidence" : 72},
    "smiling":{"value" : "false", "confidence" : 35}
}

1 Comment

gender is also an object not an array.
2

try this one.

facesDet.photos[0].tags[0].attributes.gender.value

Comments

0

getJSON return a promise not the response, you need to use this code:

$.getJSON( APIdetect, function(facesDet) {
  console.log( "success" );
  console.log(facesDet);
})

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.