0

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"}

enter image description here

Artemis
  • 589
  • 1
  • 6
  • 19
  • What are you trying to do? – Spectric Aug 12 '21 at 21:38
  • 1
    Are you calling `createHomebrew('card')` or `createHomebrew(card)` where `card` is a variable with `'card'` as value? – Kelvin Schoofs Aug 12 '21 at 21:39
  • @Spectric updated post, I'm trying to make homebrew = card.template of the json file. – Artemis Aug 12 '21 at 21:41
  • @KelvinSchoofs post updated, I'm calling a string 'card'. – Artemis Aug 12 '21 at 21:42
  • How do you know `json.homebrews[id]` is undefined? Where do you use the `homebrew` variable? – Ivar Aug 12 '21 at 21:44
  • Where does the error happen? Does it actually happen where your comment is? Or are you using it below the `$.getJSON`? – Kelvin Schoofs Aug 12 '21 at 21:45
  • @KelvinSchoofs I get it in the console as the script reach that line. – Artemis Aug 12 '21 at 21:49
  • You're going to have to provide a [mcve]. That code [should work fine](https://jsfiddle.net/h18ca9qp/1/). – Ivar Aug 12 '21 at 21:51
  • @Ivar I'm not able to reproduce it, for some reason if I write `var id = "cards"` it works, while `var id = type + 's'` does not. – Artemis Aug 12 '21 at 23:01
  • @Artemis If that change alone makes a difference, then it is safe to say that `type + 's'` produces a value that isn't either "books" or "cards". You can try to log the value of `id` to see what it actually contains. Maybe the casing is different? Or are there leading/tailing spaces? In rare occasions there might even be non-printable characters in the string that causes them to be different. In that case you can paste it into [this](https://www.soscisurvey.de/tools/view-chars.php) tool. You can also log `btoa(id)` and tell what it contains. Decoding it might show some hidden things. – Ivar Aug 12 '21 at 23:36
  • @Ivar the value is `card`, its `typeof` is String I checked but there is no typo or weird character. I tried changing "cards" to card in the json to see if the `+ s` was giving problems but no. – Artemis Aug 12 '21 at 23:43
  • @Artemis I can't help you much further I'm afraid. All I can tell you is that the error indicates that `json.homebrews` doesn't have a property whose key is the same as the value stored in the `id` variable. So either the JSON isn't what you expect it to be, or `id` isn't. The first potential issue that popped my mind was [this common one](https://stackoverflow.com/q/14220321), but that should be impossible, since you're accessing the object inside of the callback. (Unless the code you're actually running differs from the one you're showing here, in which case, it might still be the answer.) – Ivar Aug 13 '21 at 00:01
  • I’ll look into it, I just tried $ajax instead of getJSON and set async to false to see if it was the issue but with same results. – Artemis Aug 13 '21 at 00:17

1 Answers1

0

Solved, since setting id = "cards" worked, for some reason the function called with createHomebrew('card') didn't recognize card as a String, even though console.log(typeof id) returned String. So I added id = id.toString();

function createHomebrew(type) {

  var homebrew;

  $.getJSON('/data-files/templateHomebrew.json', function(json) {

   var id = type + 's';
   id = id.toString();

   homebrew = json.homebrews[id].template

  });
Artemis
  • 589
  • 1
  • 6
  • 19