Why I can't access json properties with dot (.) operator while passing a var through $.getjson()2019 Community Moderator ElectionWhy does Google prepend while(1); to their JSON responses?Mustache.js - accepts inputs as vars, but not as ajax gets: How to import or troubleshootJavaScript property access: dot notation vs. brackets?Odd results when testing boolean variable within $.getJSON functionGetting undefined when trying to acces a JSON itemAjax Pass Json to PHPFinding Item in JSON Array and adding it to another Array. JavascriptImpossible to get JSON from Service into angularJs RoutingPulling values from JSON using jQueryI am unable to render components dynamically in react.js,

Aliens englobed the Solar System: will we notice?

Look through the portal of every day

Make a transparent 448*448 image

Rejected in 4th interview round citing insufficient years of experience

Can you reject a postdoc offer after the PI has paid a large sum for flights/accommodation for your visit?

The bar has been raised

Replacing Windows 7 security updates with anti-virus?

How do I express some one as a black person?

How strictly should I take "Candidates must be local"?

In the late 1940’s to early 1950’s what technology was available that could melt a LOT of ice?

How did Alan Turing break the enigma code using the hint given by the lady in the bar?

Why is Beresheet doing a only a one-way trip?

Why doesn't this Google Translate ad use the word "Translation" instead of "Translate"?

Is "history" a male-biased word ("his+story")?

What wound would be of little consequence to a biped but terrible for a quadruped?

How to pass a string to a command that expects a file?

Do Bugbears' arms literally get longer when it's their turn?

Word for a person who has no opinion about whether god exists

Low budget alien movie about the Earth being cooked

"One can do his homework in the library"

A three room house but a three headED dog

How could our ancestors have domesticated a solitary predator?

Single word request: Harming the benefactor

Can't find the Shader/UVs tab



Why I can't access json properties with dot (.) operator while passing a var through $.getjson()



2019 Community Moderator ElectionWhy does Google prepend while(1); to their JSON responses?Mustache.js - accepts inputs as vars, but not as ajax gets: How to import or troubleshootJavaScript property access: dot notation vs. brackets?Odd results when testing boolean variable within $.getJSON functionGetting undefined when trying to acces a JSON itemAjax Pass Json to PHPFinding Item in JSON Array and adding it to another Array. JavascriptImpossible to get JSON from Service into angularJs RoutingPulling values from JSON using jQueryI am unable to render components dynamically in react.js,










3















I have a $getJSON() that i want to pass a var through how ever i always get undefined in console log.



I have done this before i just cant seem to figure out why this isnt working.



Any help would be appreciated.



here is my example







"harry":
"example": "test",
"example1": "test",

"james":
"example": "test",
"example1": "test",

"ben":
"example": "test",
"example1": "test",









 var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json.usersidno);
);

<div id="hiddenid">harry</div>












share|improve this question



















  • 1





    in which variable you get undefined?

    – Bear Nithi
    Mar 7 at 7:02











  • can you try logging 'json' object that you are receiving inside the function and see if there exists a property inside json with name useridno? as it seems there's no property named useridno inside the object.

    – Zeeshan Adil
    Mar 7 at 7:03












  • Shouldn't Harry, James, and Ben have comas between them?

    – Chipster
    Mar 7 at 7:11











  • if i console.log(json) i get all the name, as normal.

    – Mic
    Mar 7 at 7:14






  • 2





    Looks like what you're trying to do here is json[usersidno], not json.usersidno. You want to get the property whose name matches the value in usersidno, not the property named usersidno.

    – JLRishe
    Mar 7 at 7:15
















3















I have a $getJSON() that i want to pass a var through how ever i always get undefined in console log.



I have done this before i just cant seem to figure out why this isnt working.



Any help would be appreciated.



here is my example







"harry":
"example": "test",
"example1": "test",

"james":
"example": "test",
"example1": "test",

"ben":
"example": "test",
"example1": "test",









 var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json.usersidno);
);

<div id="hiddenid">harry</div>












share|improve this question



















  • 1





    in which variable you get undefined?

    – Bear Nithi
    Mar 7 at 7:02











  • can you try logging 'json' object that you are receiving inside the function and see if there exists a property inside json with name useridno? as it seems there's no property named useridno inside the object.

    – Zeeshan Adil
    Mar 7 at 7:03












  • Shouldn't Harry, James, and Ben have comas between them?

    – Chipster
    Mar 7 at 7:11











  • if i console.log(json) i get all the name, as normal.

    – Mic
    Mar 7 at 7:14






  • 2





    Looks like what you're trying to do here is json[usersidno], not json.usersidno. You want to get the property whose name matches the value in usersidno, not the property named usersidno.

    – JLRishe
    Mar 7 at 7:15














3












3








3








I have a $getJSON() that i want to pass a var through how ever i always get undefined in console log.



I have done this before i just cant seem to figure out why this isnt working.



Any help would be appreciated.



here is my example







"harry":
"example": "test",
"example1": "test",

"james":
"example": "test",
"example1": "test",

"ben":
"example": "test",
"example1": "test",









 var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json.usersidno);
);

<div id="hiddenid">harry</div>












share|improve this question
















I have a $getJSON() that i want to pass a var through how ever i always get undefined in console log.



I have done this before i just cant seem to figure out why this isnt working.



Any help would be appreciated.



here is my example







"harry":
"example": "test",
"example1": "test",

"james":
"example": "test",
"example1": "test",

"ben":
"example": "test",
"example1": "test",









 var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json.usersidno);
);

<div id="hiddenid">harry</div>









"harry":
"example": "test",
"example1": "test",

"james":
"example": "test",
"example1": "test",

"ben":
"example": "test",
"example1": "test",







"harry":
"example": "test",
"example1": "test",

"james":
"example": "test",
"example1": "test",

"ben":
"example": "test",
"example1": "test",






 var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json.usersidno);
);

<div id="hiddenid">harry</div>





 var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json.usersidno);
);

<div id="hiddenid">harry</div>






javascript jquery json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 8:43









Shubh Dixit

1




1










asked Mar 7 at 7:01









MicMic

648




648







  • 1





    in which variable you get undefined?

    – Bear Nithi
    Mar 7 at 7:02











  • can you try logging 'json' object that you are receiving inside the function and see if there exists a property inside json with name useridno? as it seems there's no property named useridno inside the object.

    – Zeeshan Adil
    Mar 7 at 7:03












  • Shouldn't Harry, James, and Ben have comas between them?

    – Chipster
    Mar 7 at 7:11











  • if i console.log(json) i get all the name, as normal.

    – Mic
    Mar 7 at 7:14






  • 2





    Looks like what you're trying to do here is json[usersidno], not json.usersidno. You want to get the property whose name matches the value in usersidno, not the property named usersidno.

    – JLRishe
    Mar 7 at 7:15













  • 1





    in which variable you get undefined?

    – Bear Nithi
    Mar 7 at 7:02











  • can you try logging 'json' object that you are receiving inside the function and see if there exists a property inside json with name useridno? as it seems there's no property named useridno inside the object.

    – Zeeshan Adil
    Mar 7 at 7:03












  • Shouldn't Harry, James, and Ben have comas between them?

    – Chipster
    Mar 7 at 7:11











  • if i console.log(json) i get all the name, as normal.

    – Mic
    Mar 7 at 7:14






  • 2





    Looks like what you're trying to do here is json[usersidno], not json.usersidno. You want to get the property whose name matches the value in usersidno, not the property named usersidno.

    – JLRishe
    Mar 7 at 7:15








1




1





in which variable you get undefined?

– Bear Nithi
Mar 7 at 7:02





in which variable you get undefined?

– Bear Nithi
Mar 7 at 7:02













can you try logging 'json' object that you are receiving inside the function and see if there exists a property inside json with name useridno? as it seems there's no property named useridno inside the object.

– Zeeshan Adil
Mar 7 at 7:03






can you try logging 'json' object that you are receiving inside the function and see if there exists a property inside json with name useridno? as it seems there's no property named useridno inside the object.

– Zeeshan Adil
Mar 7 at 7:03














Shouldn't Harry, James, and Ben have comas between them?

– Chipster
Mar 7 at 7:11





Shouldn't Harry, James, and Ben have comas between them?

– Chipster
Mar 7 at 7:11













if i console.log(json) i get all the name, as normal.

– Mic
Mar 7 at 7:14





if i console.log(json) i get all the name, as normal.

– Mic
Mar 7 at 7:14




2




2





Looks like what you're trying to do here is json[usersidno], not json.usersidno. You want to get the property whose name matches the value in usersidno, not the property named usersidno.

– JLRishe
Mar 7 at 7:15






Looks like what you're trying to do here is json[usersidno], not json.usersidno. You want to get the property whose name matches the value in usersidno, not the property named usersidno.

– JLRishe
Mar 7 at 7:15













2 Answers
2






active

oldest

votes


















3














When you want to access an object's (JSON) property dynamically (using a variable or expression), you should use square bracket notation.



[your expression or variable inside square brackets]. i.e, json[useridno]



 var usersidno = $("#hiddenid").html();
$.getJSON(
"http://localhost/example/file.json",
function(json)
console.log(json[usersidno]); // try to use square brackets
);





share|improve this answer






























    0














    This is a working example of ,your issue I have used an api ,that gives back a json ,You can use [] annotatiton to access object properties.






    $(document).ready(function()

    var user = $("#hiddenid").html()
    console.log(typeof user)

    $.getJSON(
    "https://jsonplaceholder.typicode.com/todos/1",
    function(json)
    let js=JSON.parse(JSON.stringify(json))
    console.log(js[user]);
    console.log(js.user);//undefined
    console.log(js)

    );

    );

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="hiddenid">userId</div>





    Explanation:



    var userid = 'userId';
    console.log(x.userid); // it looks for x.userid, which throws undefined,if it does'nt exists
    console.log(x[userid]); // looks for x['userId'] which it will find


    Have a look at mdn docs






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55037844%2fwhy-i-cant-access-json-properties-with-dot-operator-while-passing-a-var-thr%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      When you want to access an object's (JSON) property dynamically (using a variable or expression), you should use square bracket notation.



      [your expression or variable inside square brackets]. i.e, json[useridno]



       var usersidno = $("#hiddenid").html();
      $.getJSON(
      "http://localhost/example/file.json",
      function(json)
      console.log(json[usersidno]); // try to use square brackets
      );





      share|improve this answer



























        3














        When you want to access an object's (JSON) property dynamically (using a variable or expression), you should use square bracket notation.



        [your expression or variable inside square brackets]. i.e, json[useridno]



         var usersidno = $("#hiddenid").html();
        $.getJSON(
        "http://localhost/example/file.json",
        function(json)
        console.log(json[usersidno]); // try to use square brackets
        );





        share|improve this answer

























          3












          3








          3







          When you want to access an object's (JSON) property dynamically (using a variable or expression), you should use square bracket notation.



          [your expression or variable inside square brackets]. i.e, json[useridno]



           var usersidno = $("#hiddenid").html();
          $.getJSON(
          "http://localhost/example/file.json",
          function(json)
          console.log(json[usersidno]); // try to use square brackets
          );





          share|improve this answer













          When you want to access an object's (JSON) property dynamically (using a variable or expression), you should use square bracket notation.



          [your expression or variable inside square brackets]. i.e, json[useridno]



           var usersidno = $("#hiddenid").html();
          $.getJSON(
          "http://localhost/example/file.json",
          function(json)
          console.log(json[usersidno]); // try to use square brackets
          );






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 7 at 7:17









          Bear NithiBear Nithi

          2,596723




          2,596723























              0














              This is a working example of ,your issue I have used an api ,that gives back a json ,You can use [] annotatiton to access object properties.






              $(document).ready(function()

              var user = $("#hiddenid").html()
              console.log(typeof user)

              $.getJSON(
              "https://jsonplaceholder.typicode.com/todos/1",
              function(json)
              let js=JSON.parse(JSON.stringify(json))
              console.log(js[user]);
              console.log(js.user);//undefined
              console.log(js)

              );

              );

              <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
              <div id="hiddenid">userId</div>





              Explanation:



              var userid = 'userId';
              console.log(x.userid); // it looks for x.userid, which throws undefined,if it does'nt exists
              console.log(x[userid]); // looks for x['userId'] which it will find


              Have a look at mdn docs






              share|improve this answer





























                0














                This is a working example of ,your issue I have used an api ,that gives back a json ,You can use [] annotatiton to access object properties.






                $(document).ready(function()

                var user = $("#hiddenid").html()
                console.log(typeof user)

                $.getJSON(
                "https://jsonplaceholder.typicode.com/todos/1",
                function(json)
                let js=JSON.parse(JSON.stringify(json))
                console.log(js[user]);
                console.log(js.user);//undefined
                console.log(js)

                );

                );

                <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                <div id="hiddenid">userId</div>





                Explanation:



                var userid = 'userId';
                console.log(x.userid); // it looks for x.userid, which throws undefined,if it does'nt exists
                console.log(x[userid]); // looks for x['userId'] which it will find


                Have a look at mdn docs






                share|improve this answer



























                  0












                  0








                  0







                  This is a working example of ,your issue I have used an api ,that gives back a json ,You can use [] annotatiton to access object properties.






                  $(document).ready(function()

                  var user = $("#hiddenid").html()
                  console.log(typeof user)

                  $.getJSON(
                  "https://jsonplaceholder.typicode.com/todos/1",
                  function(json)
                  let js=JSON.parse(JSON.stringify(json))
                  console.log(js[user]);
                  console.log(js.user);//undefined
                  console.log(js)

                  );

                  );

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                  <div id="hiddenid">userId</div>





                  Explanation:



                  var userid = 'userId';
                  console.log(x.userid); // it looks for x.userid, which throws undefined,if it does'nt exists
                  console.log(x[userid]); // looks for x['userId'] which it will find


                  Have a look at mdn docs






                  share|improve this answer















                  This is a working example of ,your issue I have used an api ,that gives back a json ,You can use [] annotatiton to access object properties.






                  $(document).ready(function()

                  var user = $("#hiddenid").html()
                  console.log(typeof user)

                  $.getJSON(
                  "https://jsonplaceholder.typicode.com/todos/1",
                  function(json)
                  let js=JSON.parse(JSON.stringify(json))
                  console.log(js[user]);
                  console.log(js.user);//undefined
                  console.log(js)

                  );

                  );

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                  <div id="hiddenid">userId</div>





                  Explanation:



                  var userid = 'userId';
                  console.log(x.userid); // it looks for x.userid, which throws undefined,if it does'nt exists
                  console.log(x[userid]); // looks for x['userId'] which it will find


                  Have a look at mdn docs






                  $(document).ready(function()

                  var user = $("#hiddenid").html()
                  console.log(typeof user)

                  $.getJSON(
                  "https://jsonplaceholder.typicode.com/todos/1",
                  function(json)
                  let js=JSON.parse(JSON.stringify(json))
                  console.log(js[user]);
                  console.log(js.user);//undefined
                  console.log(js)

                  );

                  );

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                  <div id="hiddenid">userId</div>





                  $(document).ready(function()

                  var user = $("#hiddenid").html()
                  console.log(typeof user)

                  $.getJSON(
                  "https://jsonplaceholder.typicode.com/todos/1",
                  function(json)
                  let js=JSON.parse(JSON.stringify(json))
                  console.log(js[user]);
                  console.log(js.user);//undefined
                  console.log(js)

                  );

                  );

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                  <div id="hiddenid">userId</div>






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 7 at 7:50









                  Cody Gray

                  194k35381469




                  194k35381469










                  answered Mar 7 at 7:24









                  Shubh DixitShubh Dixit

                  1




                  1



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55037844%2fwhy-i-cant-access-json-properties-with-dot-operator-while-passing-a-var-thr%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                      2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

                      Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme