MagicMirror Forum
    • Recent
    • Tags
    • Unsolved
    • Solved
    • MagicMirror² Repository
    • Documentation
    • 3rd-Party-Modules
    • Donate
    • Discord
    • Register
    • Login
    A New Chapter for MagicMirror: The Community Takes the Lead
    Read the statement by Michael Teeuw here.

    trouble Accessing JSON structure using variables, help

    Scheduled Pinned Locked Moved Solved Troubleshooting
    4 Posts 2 Posters 1.1k Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S Offline
      sdetweil
      last edited by yawns

      I am trying to access a json structure using variables containing the parts of the structure.

      according to the javascript and json docs, when u want to use variables, u must use the bracket notation

      so

      object[key1][key2].value

      when object looks like this

      {
        "fred":{"mary":"value1","sue":value2"}
      }
      

      so

      var a='fred'
      var b='sue'
      

      and

      object[a][b].value should return 'value2'
      

      but in my module, I am getting cannot access 'sue' of undefined
      if I make a variable then it works, but still throws an error

      var bob=object[a][b] returns error cannot access 'sue' of undefined

      but if I do
      var bob= object[a.toString()][b.toString()]

      then it works, and bob= the sue data item and
      bob.value works… BUT still throws an error cannot access value of undefined… but it worked (I debug step thru the code)…

      if I use literals instead of variables as the key names, then it works… but I need to use variables to reduce the code by using a loop to walk thru the big json object…

      do we have some downlevel javascript? or??

      this should be SO easy, but something is weird…

      Sam

      How to add modules

      learning how to use browser developers window for css changes

      S 1 Reply Last reply Reply Quote 0
      • S Offline
        sdetweil @sdetweil
        last edited by sdetweil

        @sdetweil well, its a side effect of the way the code works…

        if (object[key][key] !==‘undefined’)

        will throw an access error if either of the two keys are not in the object… BEFORE you can check the results with the if…

        SO, wrap with try/catch and all is well…

        Sam

        How to add modules

        learning how to use browser developers window for css changes

        Mykle1M 1 Reply Last reply Reply Quote 1
        • Mykle1M Offline
          Mykle1 Project Sponsor Module Developer @sdetweil
          last edited by

          @sdetweil

          You genius types are awesome! :thumbsup:

          Create the topic. Reply to yourself. Mark topic solved (by self).

          Create a working config
          How to add modules

          S 1 Reply Last reply Reply Quote 0
          • S Offline
            sdetweil @Mykle1
            last edited by

            @mykle1 there is no better thought motivator than posting on a live forum…!!

            Sam

            How to add modules

            learning how to use browser developers window for css changes

            1 Reply Last reply Reply Quote 1
            • 1 / 1
            • First post
              Last post
            Enjoying MagicMirror? Please consider a donation!
            MagicMirror created by Michael Teeuw.
            Forum managed by Sam, technical setup by Karsten.
            This forum is using NodeBB as its core | Contributors
            Contact | Privacy Policy