Tejas Girase Tejas Girase - 5 months ago 9
Javascript Question

how get value of JSON object within Json ..?

Please Help

"_attachments": {
"kiran.jpg": {
"content_type": "image/jpeg",
"revpos": 6,
"digest": "md5-mEsoX4ljN1iJlF2bX1Lw2g==",
"length": 4601,
"stub": true
}
}


I want to value of content_type and length.
how to get that.
friends i dont know the value of kiran.jpg, its come random image name form database

Answer

Use object.keys to loop over the keys in the _attachments object to find the name of each record.

var data = {
    "_attachments": {
       "kiran.jpg": {
           "content_type": "image/jpeg",
           "revpos": 6,
           "digest": "md5-mEsoX4ljN1iJlF2bX1Lw2g==",
           "length": 4601,
           "stub": true
       },
       "otherPerson.jpg" : {
           "content_type": "image/jpeg",
           "revpos": 8,
           "digest": "md5-mE4ljdfhgfh1iJlF2bX1Lw2g==",
           "length": 1337,
           "stub": false       
       }
   }
};

Object.keys(data._attachments).forEach(function( name ) {
    var contentType = data._attachments[name].content_type;
    console.log(contentType);
});
Comments