I'm trying to read the property of a json object using variables. If I use variables i get error, while if I use properties it works.
JSON:
{
"homebrews": {
"books": {
"title": "text."
},
"cards": {
"template": {
"id": 0,
"name": "myName"
}
}
}
}
Function called
createHomebrew('card');
function:
function createHomebrew(type) {
var homebrew;
$.getJSON('/data-files/templateHomebrew.json', function(json) {
var id = type + 's'; // cards
homebrew = json.homebrews[id].template // json.homebrews[id] is undefined
});
Instead
console.log(json.homebrews.cards.template); // Object { id: 0, name: "myName"}