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

                      Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

                      Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

                      How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page