Javascript / Json Requested Object returns null












0















i'm trying to access a local JSON File with JS, turn it into an JS Object by parsing it and logging it to the console. I'm using Live Server in VS Code to set up the Server, therefore the localhost URL.



    var requestURL = "http://localhost:5500/sqVol.json";
var request = new XMLHttpRequest();

request.open('GET', requestURL);
request.responseType = 'json';
request.send();

var jsonObj = request.response;
request.onload = function () {
JSON.parse(jsonObj);
logData(jsonObj);
};

function logData(jsonObj){
console.log("jsonObj= " + jsonObj);
//console.log("jsonObj[Datum]= " + jsonObj[Datum]);
//console.log("jsonObj.Datum= " + jsonObj.Datum);
}



Output: jsonObj= null




The JSON File:



  {
"Datum": ["03.05.2017","05.06.2017"],
"Volume": [1338,1445]
}


Here's what I imagine happens:
I'm getting the Answer from localhost and parsing it via JSON.parse into an JS Object. As soon as the request finished im calling my logData function and passing the parsed Data to log it.










share|improve this question




















  • 1





    Possible duplicate of How do I return the response from an asynchronous call?

    – connexo
    Nov 16 '18 at 7:05











  • You're just parsing JSON too many times. remove one JSON.parse and you might get it right because JSON.parse expects a string and you're passing an object.

    – Mani Kumar Reddy Kancharla
    Nov 16 '18 at 7:06











  • var jsonObj = JSON.parse(request.response); at this time response is null or undefined. You fail to understand the asynchronous nature of your call.

    – connexo
    Nov 16 '18 at 7:07













  • Removed it - doesn't change anything unfortunately. edit: and put parse into the onload function. No change either. :(

    – Aa Ron
    Nov 16 '18 at 7:07








  • 1





    @AaRon you have given responseType = "json" already before sending the request so request.response is already a javascript object and not a JSON string. So ignore all the code after request.send() and just use console.log(request.response) inside onload function which should be declared bold before request.send() executes.

    – Mani Kumar Reddy Kancharla
    Nov 16 '18 at 7:12


















0















i'm trying to access a local JSON File with JS, turn it into an JS Object by parsing it and logging it to the console. I'm using Live Server in VS Code to set up the Server, therefore the localhost URL.



    var requestURL = "http://localhost:5500/sqVol.json";
var request = new XMLHttpRequest();

request.open('GET', requestURL);
request.responseType = 'json';
request.send();

var jsonObj = request.response;
request.onload = function () {
JSON.parse(jsonObj);
logData(jsonObj);
};

function logData(jsonObj){
console.log("jsonObj= " + jsonObj);
//console.log("jsonObj[Datum]= " + jsonObj[Datum]);
//console.log("jsonObj.Datum= " + jsonObj.Datum);
}



Output: jsonObj= null




The JSON File:



  {
"Datum": ["03.05.2017","05.06.2017"],
"Volume": [1338,1445]
}


Here's what I imagine happens:
I'm getting the Answer from localhost and parsing it via JSON.parse into an JS Object. As soon as the request finished im calling my logData function and passing the parsed Data to log it.










share|improve this question




















  • 1





    Possible duplicate of How do I return the response from an asynchronous call?

    – connexo
    Nov 16 '18 at 7:05











  • You're just parsing JSON too many times. remove one JSON.parse and you might get it right because JSON.parse expects a string and you're passing an object.

    – Mani Kumar Reddy Kancharla
    Nov 16 '18 at 7:06











  • var jsonObj = JSON.parse(request.response); at this time response is null or undefined. You fail to understand the asynchronous nature of your call.

    – connexo
    Nov 16 '18 at 7:07













  • Removed it - doesn't change anything unfortunately. edit: and put parse into the onload function. No change either. :(

    – Aa Ron
    Nov 16 '18 at 7:07








  • 1





    @AaRon you have given responseType = "json" already before sending the request so request.response is already a javascript object and not a JSON string. So ignore all the code after request.send() and just use console.log(request.response) inside onload function which should be declared bold before request.send() executes.

    – Mani Kumar Reddy Kancharla
    Nov 16 '18 at 7:12
















0












0








0








i'm trying to access a local JSON File with JS, turn it into an JS Object by parsing it and logging it to the console. I'm using Live Server in VS Code to set up the Server, therefore the localhost URL.



    var requestURL = "http://localhost:5500/sqVol.json";
var request = new XMLHttpRequest();

request.open('GET', requestURL);
request.responseType = 'json';
request.send();

var jsonObj = request.response;
request.onload = function () {
JSON.parse(jsonObj);
logData(jsonObj);
};

function logData(jsonObj){
console.log("jsonObj= " + jsonObj);
//console.log("jsonObj[Datum]= " + jsonObj[Datum]);
//console.log("jsonObj.Datum= " + jsonObj.Datum);
}



Output: jsonObj= null




The JSON File:



  {
"Datum": ["03.05.2017","05.06.2017"],
"Volume": [1338,1445]
}


Here's what I imagine happens:
I'm getting the Answer from localhost and parsing it via JSON.parse into an JS Object. As soon as the request finished im calling my logData function and passing the parsed Data to log it.










share|improve this question
















i'm trying to access a local JSON File with JS, turn it into an JS Object by parsing it and logging it to the console. I'm using Live Server in VS Code to set up the Server, therefore the localhost URL.



    var requestURL = "http://localhost:5500/sqVol.json";
var request = new XMLHttpRequest();

request.open('GET', requestURL);
request.responseType = 'json';
request.send();

var jsonObj = request.response;
request.onload = function () {
JSON.parse(jsonObj);
logData(jsonObj);
};

function logData(jsonObj){
console.log("jsonObj= " + jsonObj);
//console.log("jsonObj[Datum]= " + jsonObj[Datum]);
//console.log("jsonObj.Datum= " + jsonObj.Datum);
}



Output: jsonObj= null




The JSON File:



  {
"Datum": ["03.05.2017","05.06.2017"],
"Volume": [1338,1445]
}


Here's what I imagine happens:
I'm getting the Answer from localhost and parsing it via JSON.parse into an JS Object. As soon as the request finished im calling my logData function and passing the parsed Data to log it.







javascript json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 7:09







Aa Ron

















asked Nov 16 '18 at 7:01









Aa RonAa Ron

162




162








  • 1





    Possible duplicate of How do I return the response from an asynchronous call?

    – connexo
    Nov 16 '18 at 7:05











  • You're just parsing JSON too many times. remove one JSON.parse and you might get it right because JSON.parse expects a string and you're passing an object.

    – Mani Kumar Reddy Kancharla
    Nov 16 '18 at 7:06











  • var jsonObj = JSON.parse(request.response); at this time response is null or undefined. You fail to understand the asynchronous nature of your call.

    – connexo
    Nov 16 '18 at 7:07













  • Removed it - doesn't change anything unfortunately. edit: and put parse into the onload function. No change either. :(

    – Aa Ron
    Nov 16 '18 at 7:07








  • 1





    @AaRon you have given responseType = "json" already before sending the request so request.response is already a javascript object and not a JSON string. So ignore all the code after request.send() and just use console.log(request.response) inside onload function which should be declared bold before request.send() executes.

    – Mani Kumar Reddy Kancharla
    Nov 16 '18 at 7:12
















  • 1





    Possible duplicate of How do I return the response from an asynchronous call?

    – connexo
    Nov 16 '18 at 7:05











  • You're just parsing JSON too many times. remove one JSON.parse and you might get it right because JSON.parse expects a string and you're passing an object.

    – Mani Kumar Reddy Kancharla
    Nov 16 '18 at 7:06











  • var jsonObj = JSON.parse(request.response); at this time response is null or undefined. You fail to understand the asynchronous nature of your call.

    – connexo
    Nov 16 '18 at 7:07













  • Removed it - doesn't change anything unfortunately. edit: and put parse into the onload function. No change either. :(

    – Aa Ron
    Nov 16 '18 at 7:07








  • 1





    @AaRon you have given responseType = "json" already before sending the request so request.response is already a javascript object and not a JSON string. So ignore all the code after request.send() and just use console.log(request.response) inside onload function which should be declared bold before request.send() executes.

    – Mani Kumar Reddy Kancharla
    Nov 16 '18 at 7:12










1




1





Possible duplicate of How do I return the response from an asynchronous call?

– connexo
Nov 16 '18 at 7:05





Possible duplicate of How do I return the response from an asynchronous call?

– connexo
Nov 16 '18 at 7:05













You're just parsing JSON too many times. remove one JSON.parse and you might get it right because JSON.parse expects a string and you're passing an object.

– Mani Kumar Reddy Kancharla
Nov 16 '18 at 7:06





You're just parsing JSON too many times. remove one JSON.parse and you might get it right because JSON.parse expects a string and you're passing an object.

– Mani Kumar Reddy Kancharla
Nov 16 '18 at 7:06













var jsonObj = JSON.parse(request.response); at this time response is null or undefined. You fail to understand the asynchronous nature of your call.

– connexo
Nov 16 '18 at 7:07







var jsonObj = JSON.parse(request.response); at this time response is null or undefined. You fail to understand the asynchronous nature of your call.

– connexo
Nov 16 '18 at 7:07















Removed it - doesn't change anything unfortunately. edit: and put parse into the onload function. No change either. :(

– Aa Ron
Nov 16 '18 at 7:07







Removed it - doesn't change anything unfortunately. edit: and put parse into the onload function. No change either. :(

– Aa Ron
Nov 16 '18 at 7:07






1




1





@AaRon you have given responseType = "json" already before sending the request so request.response is already a javascript object and not a JSON string. So ignore all the code after request.send() and just use console.log(request.response) inside onload function which should be declared bold before request.send() executes.

– Mani Kumar Reddy Kancharla
Nov 16 '18 at 7:12







@AaRon you have given responseType = "json" already before sending the request so request.response is already a javascript object and not a JSON string. So ignore all the code after request.send() and just use console.log(request.response) inside onload function which should be declared bold before request.send() executes.

– Mani Kumar Reddy Kancharla
Nov 16 '18 at 7:12














3 Answers
3






active

oldest

votes


















1














As @connexo pointed out I didn't understand the asynchronous nature of the call. And frankly i still don't but i guess a good starting points will be:



How do I return the response from an asynchronous call?



MDN Synchronous and Asynchronous Requests



As @Mani Kumar Reddy Kancharla pointed out my response is already a JS Object since i declared request.responseType = 'json';



This is how it looks right now:



var requestURL = "http://localhost:5500/sqVol.json";
var request = new XMLHttpRequest();

request.open('GET', requestURL);
request.responseType = 'json';
request.send();

request.onload = function () {
console.log(request.response);
var jsonObj = request.response;
logData(jsonObj);
};

function logData(jsonObj){
console.log("jsonObj= " + jsonObj);
console.log("jsonObj[Datum]= " + jsonObj.Datum);



Ouput: {…} ​
Datum: Array [ "03.05.2017", "05.06.2017" ] ​
Volume: Array [ 1338, 1445 ] ​



jsonObj= [object Object]

jsonObj[Datum]= 03.05.2017,05.06.2017




As you can see i can also access the .Datum Property. I Consider it solved. Thank you!



edit: Added the link provided by connexo.






share|improve this answer


























  • Hey @AaRon checkout my answer, it also solves the jsonObj issue by declaring it before onload function rather than inside onload function to solve the scope issue so that it can be used inside logData function

    – Mani Kumar Reddy Kancharla
    Nov 16 '18 at 7:52



















0














I used the browser XMLHttpRequest object for Ajax about 12 years ago.



https://api.jquery.com/jquery.getjson/






share|improve this answer































    0














    You have two issues in your code:




    1. You're using JSON.parse() too many times on same date which is already converted from JSON string to a JavaScript object and tries to parse a Javascript object raising a Syntax error. You've already mention request.responseType = 'json' i.e. responseType specifies you're receiving a JSON string as response so JavaScript Engine will already parse it and provide you a JavaScript object as request.response and you need not parse it. So you could use JSON.parse(request.responseText) if you want to parse it yourselves or directly use request.response as a JavaScript object.

    2. You're trying to store request.response into a variable outside the load function which will just provide the value of request.response at that time which could be null if the response is not received yet. You need to get the response in the onload fucntion implementation as it is executed once you receive the response.


    So what you're looking for overall is this ->



    var requestURL = "http://localhost:5500/sqVol.json";
    var request = new XMLHttpRequest();
    var jsonObj;

    function logData(jsonObj){
    console.log("jsonObj= " + JSON.stringify(jsonObj));
    console.log("jsonObj[Datum]= " + JSON.stringify(jsonObj["Datum"]));
    console.log("jsonObj.Datum= " + JSON.stringify(jsonObj.Datum));
    // or simply
    console.log(jsonObj);
    console.log(jsonObj['Datum']);
    console.log(jsonObj.Datum);
    }

    request.onload = function () {
    jsonObj = request.response;
    logData(jsonObj);
    };

    request.responseType = 'json';

    // finally send the request
    request.open('GET', requestURL);
    request.send();


    Here JSON.stringify() converts a JavaScript Object back to a JSON string.



    Refer to this link to get more information on how to use XMLHttpRequest



    EDIT: Referring to another answer posted, people simply use libraries like jQuery AJAX to make asynchronous requests to get data from the web, especially in XML or JSON format.






    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%2f53332934%2fjavascript-json-requested-object-returns-null%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      As @connexo pointed out I didn't understand the asynchronous nature of the call. And frankly i still don't but i guess a good starting points will be:



      How do I return the response from an asynchronous call?



      MDN Synchronous and Asynchronous Requests



      As @Mani Kumar Reddy Kancharla pointed out my response is already a JS Object since i declared request.responseType = 'json';



      This is how it looks right now:



      var requestURL = "http://localhost:5500/sqVol.json";
      var request = new XMLHttpRequest();

      request.open('GET', requestURL);
      request.responseType = 'json';
      request.send();

      request.onload = function () {
      console.log(request.response);
      var jsonObj = request.response;
      logData(jsonObj);
      };

      function logData(jsonObj){
      console.log("jsonObj= " + jsonObj);
      console.log("jsonObj[Datum]= " + jsonObj.Datum);



      Ouput: {…} ​
      Datum: Array [ "03.05.2017", "05.06.2017" ] ​
      Volume: Array [ 1338, 1445 ] ​



      jsonObj= [object Object]

      jsonObj[Datum]= 03.05.2017,05.06.2017




      As you can see i can also access the .Datum Property. I Consider it solved. Thank you!



      edit: Added the link provided by connexo.






      share|improve this answer


























      • Hey @AaRon checkout my answer, it also solves the jsonObj issue by declaring it before onload function rather than inside onload function to solve the scope issue so that it can be used inside logData function

        – Mani Kumar Reddy Kancharla
        Nov 16 '18 at 7:52
















      1














      As @connexo pointed out I didn't understand the asynchronous nature of the call. And frankly i still don't but i guess a good starting points will be:



      How do I return the response from an asynchronous call?



      MDN Synchronous and Asynchronous Requests



      As @Mani Kumar Reddy Kancharla pointed out my response is already a JS Object since i declared request.responseType = 'json';



      This is how it looks right now:



      var requestURL = "http://localhost:5500/sqVol.json";
      var request = new XMLHttpRequest();

      request.open('GET', requestURL);
      request.responseType = 'json';
      request.send();

      request.onload = function () {
      console.log(request.response);
      var jsonObj = request.response;
      logData(jsonObj);
      };

      function logData(jsonObj){
      console.log("jsonObj= " + jsonObj);
      console.log("jsonObj[Datum]= " + jsonObj.Datum);



      Ouput: {…} ​
      Datum: Array [ "03.05.2017", "05.06.2017" ] ​
      Volume: Array [ 1338, 1445 ] ​



      jsonObj= [object Object]

      jsonObj[Datum]= 03.05.2017,05.06.2017




      As you can see i can also access the .Datum Property. I Consider it solved. Thank you!



      edit: Added the link provided by connexo.






      share|improve this answer


























      • Hey @AaRon checkout my answer, it also solves the jsonObj issue by declaring it before onload function rather than inside onload function to solve the scope issue so that it can be used inside logData function

        – Mani Kumar Reddy Kancharla
        Nov 16 '18 at 7:52














      1












      1








      1







      As @connexo pointed out I didn't understand the asynchronous nature of the call. And frankly i still don't but i guess a good starting points will be:



      How do I return the response from an asynchronous call?



      MDN Synchronous and Asynchronous Requests



      As @Mani Kumar Reddy Kancharla pointed out my response is already a JS Object since i declared request.responseType = 'json';



      This is how it looks right now:



      var requestURL = "http://localhost:5500/sqVol.json";
      var request = new XMLHttpRequest();

      request.open('GET', requestURL);
      request.responseType = 'json';
      request.send();

      request.onload = function () {
      console.log(request.response);
      var jsonObj = request.response;
      logData(jsonObj);
      };

      function logData(jsonObj){
      console.log("jsonObj= " + jsonObj);
      console.log("jsonObj[Datum]= " + jsonObj.Datum);



      Ouput: {…} ​
      Datum: Array [ "03.05.2017", "05.06.2017" ] ​
      Volume: Array [ 1338, 1445 ] ​



      jsonObj= [object Object]

      jsonObj[Datum]= 03.05.2017,05.06.2017




      As you can see i can also access the .Datum Property. I Consider it solved. Thank you!



      edit: Added the link provided by connexo.






      share|improve this answer















      As @connexo pointed out I didn't understand the asynchronous nature of the call. And frankly i still don't but i guess a good starting points will be:



      How do I return the response from an asynchronous call?



      MDN Synchronous and Asynchronous Requests



      As @Mani Kumar Reddy Kancharla pointed out my response is already a JS Object since i declared request.responseType = 'json';



      This is how it looks right now:



      var requestURL = "http://localhost:5500/sqVol.json";
      var request = new XMLHttpRequest();

      request.open('GET', requestURL);
      request.responseType = 'json';
      request.send();

      request.onload = function () {
      console.log(request.response);
      var jsonObj = request.response;
      logData(jsonObj);
      };

      function logData(jsonObj){
      console.log("jsonObj= " + jsonObj);
      console.log("jsonObj[Datum]= " + jsonObj.Datum);



      Ouput: {…} ​
      Datum: Array [ "03.05.2017", "05.06.2017" ] ​
      Volume: Array [ 1338, 1445 ] ​



      jsonObj= [object Object]

      jsonObj[Datum]= 03.05.2017,05.06.2017




      As you can see i can also access the .Datum Property. I Consider it solved. Thank you!



      edit: Added the link provided by connexo.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 16 '18 at 7:43

























      answered Nov 16 '18 at 7:31









      Aa RonAa Ron

      162




      162













      • Hey @AaRon checkout my answer, it also solves the jsonObj issue by declaring it before onload function rather than inside onload function to solve the scope issue so that it can be used inside logData function

        – Mani Kumar Reddy Kancharla
        Nov 16 '18 at 7:52



















      • Hey @AaRon checkout my answer, it also solves the jsonObj issue by declaring it before onload function rather than inside onload function to solve the scope issue so that it can be used inside logData function

        – Mani Kumar Reddy Kancharla
        Nov 16 '18 at 7:52

















      Hey @AaRon checkout my answer, it also solves the jsonObj issue by declaring it before onload function rather than inside onload function to solve the scope issue so that it can be used inside logData function

      – Mani Kumar Reddy Kancharla
      Nov 16 '18 at 7:52





      Hey @AaRon checkout my answer, it also solves the jsonObj issue by declaring it before onload function rather than inside onload function to solve the scope issue so that it can be used inside logData function

      – Mani Kumar Reddy Kancharla
      Nov 16 '18 at 7:52













      0














      I used the browser XMLHttpRequest object for Ajax about 12 years ago.



      https://api.jquery.com/jquery.getjson/






      share|improve this answer




























        0














        I used the browser XMLHttpRequest object for Ajax about 12 years ago.



        https://api.jquery.com/jquery.getjson/






        share|improve this answer


























          0












          0








          0







          I used the browser XMLHttpRequest object for Ajax about 12 years ago.



          https://api.jquery.com/jquery.getjson/






          share|improve this answer













          I used the browser XMLHttpRequest object for Ajax about 12 years ago.



          https://api.jquery.com/jquery.getjson/







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 7:53









          Dudeman3000Dudeman3000

          124213




          124213























              0














              You have two issues in your code:




              1. You're using JSON.parse() too many times on same date which is already converted from JSON string to a JavaScript object and tries to parse a Javascript object raising a Syntax error. You've already mention request.responseType = 'json' i.e. responseType specifies you're receiving a JSON string as response so JavaScript Engine will already parse it and provide you a JavaScript object as request.response and you need not parse it. So you could use JSON.parse(request.responseText) if you want to parse it yourselves or directly use request.response as a JavaScript object.

              2. You're trying to store request.response into a variable outside the load function which will just provide the value of request.response at that time which could be null if the response is not received yet. You need to get the response in the onload fucntion implementation as it is executed once you receive the response.


              So what you're looking for overall is this ->



              var requestURL = "http://localhost:5500/sqVol.json";
              var request = new XMLHttpRequest();
              var jsonObj;

              function logData(jsonObj){
              console.log("jsonObj= " + JSON.stringify(jsonObj));
              console.log("jsonObj[Datum]= " + JSON.stringify(jsonObj["Datum"]));
              console.log("jsonObj.Datum= " + JSON.stringify(jsonObj.Datum));
              // or simply
              console.log(jsonObj);
              console.log(jsonObj['Datum']);
              console.log(jsonObj.Datum);
              }

              request.onload = function () {
              jsonObj = request.response;
              logData(jsonObj);
              };

              request.responseType = 'json';

              // finally send the request
              request.open('GET', requestURL);
              request.send();


              Here JSON.stringify() converts a JavaScript Object back to a JSON string.



              Refer to this link to get more information on how to use XMLHttpRequest



              EDIT: Referring to another answer posted, people simply use libraries like jQuery AJAX to make asynchronous requests to get data from the web, especially in XML or JSON format.






              share|improve this answer






























                0














                You have two issues in your code:




                1. You're using JSON.parse() too many times on same date which is already converted from JSON string to a JavaScript object and tries to parse a Javascript object raising a Syntax error. You've already mention request.responseType = 'json' i.e. responseType specifies you're receiving a JSON string as response so JavaScript Engine will already parse it and provide you a JavaScript object as request.response and you need not parse it. So you could use JSON.parse(request.responseText) if you want to parse it yourselves or directly use request.response as a JavaScript object.

                2. You're trying to store request.response into a variable outside the load function which will just provide the value of request.response at that time which could be null if the response is not received yet. You need to get the response in the onload fucntion implementation as it is executed once you receive the response.


                So what you're looking for overall is this ->



                var requestURL = "http://localhost:5500/sqVol.json";
                var request = new XMLHttpRequest();
                var jsonObj;

                function logData(jsonObj){
                console.log("jsonObj= " + JSON.stringify(jsonObj));
                console.log("jsonObj[Datum]= " + JSON.stringify(jsonObj["Datum"]));
                console.log("jsonObj.Datum= " + JSON.stringify(jsonObj.Datum));
                // or simply
                console.log(jsonObj);
                console.log(jsonObj['Datum']);
                console.log(jsonObj.Datum);
                }

                request.onload = function () {
                jsonObj = request.response;
                logData(jsonObj);
                };

                request.responseType = 'json';

                // finally send the request
                request.open('GET', requestURL);
                request.send();


                Here JSON.stringify() converts a JavaScript Object back to a JSON string.



                Refer to this link to get more information on how to use XMLHttpRequest



                EDIT: Referring to another answer posted, people simply use libraries like jQuery AJAX to make asynchronous requests to get data from the web, especially in XML or JSON format.






                share|improve this answer




























                  0












                  0








                  0







                  You have two issues in your code:




                  1. You're using JSON.parse() too many times on same date which is already converted from JSON string to a JavaScript object and tries to parse a Javascript object raising a Syntax error. You've already mention request.responseType = 'json' i.e. responseType specifies you're receiving a JSON string as response so JavaScript Engine will already parse it and provide you a JavaScript object as request.response and you need not parse it. So you could use JSON.parse(request.responseText) if you want to parse it yourselves or directly use request.response as a JavaScript object.

                  2. You're trying to store request.response into a variable outside the load function which will just provide the value of request.response at that time which could be null if the response is not received yet. You need to get the response in the onload fucntion implementation as it is executed once you receive the response.


                  So what you're looking for overall is this ->



                  var requestURL = "http://localhost:5500/sqVol.json";
                  var request = new XMLHttpRequest();
                  var jsonObj;

                  function logData(jsonObj){
                  console.log("jsonObj= " + JSON.stringify(jsonObj));
                  console.log("jsonObj[Datum]= " + JSON.stringify(jsonObj["Datum"]));
                  console.log("jsonObj.Datum= " + JSON.stringify(jsonObj.Datum));
                  // or simply
                  console.log(jsonObj);
                  console.log(jsonObj['Datum']);
                  console.log(jsonObj.Datum);
                  }

                  request.onload = function () {
                  jsonObj = request.response;
                  logData(jsonObj);
                  };

                  request.responseType = 'json';

                  // finally send the request
                  request.open('GET', requestURL);
                  request.send();


                  Here JSON.stringify() converts a JavaScript Object back to a JSON string.



                  Refer to this link to get more information on how to use XMLHttpRequest



                  EDIT: Referring to another answer posted, people simply use libraries like jQuery AJAX to make asynchronous requests to get data from the web, especially in XML or JSON format.






                  share|improve this answer















                  You have two issues in your code:




                  1. You're using JSON.parse() too many times on same date which is already converted from JSON string to a JavaScript object and tries to parse a Javascript object raising a Syntax error. You've already mention request.responseType = 'json' i.e. responseType specifies you're receiving a JSON string as response so JavaScript Engine will already parse it and provide you a JavaScript object as request.response and you need not parse it. So you could use JSON.parse(request.responseText) if you want to parse it yourselves or directly use request.response as a JavaScript object.

                  2. You're trying to store request.response into a variable outside the load function which will just provide the value of request.response at that time which could be null if the response is not received yet. You need to get the response in the onload fucntion implementation as it is executed once you receive the response.


                  So what you're looking for overall is this ->



                  var requestURL = "http://localhost:5500/sqVol.json";
                  var request = new XMLHttpRequest();
                  var jsonObj;

                  function logData(jsonObj){
                  console.log("jsonObj= " + JSON.stringify(jsonObj));
                  console.log("jsonObj[Datum]= " + JSON.stringify(jsonObj["Datum"]));
                  console.log("jsonObj.Datum= " + JSON.stringify(jsonObj.Datum));
                  // or simply
                  console.log(jsonObj);
                  console.log(jsonObj['Datum']);
                  console.log(jsonObj.Datum);
                  }

                  request.onload = function () {
                  jsonObj = request.response;
                  logData(jsonObj);
                  };

                  request.responseType = 'json';

                  // finally send the request
                  request.open('GET', requestURL);
                  request.send();


                  Here JSON.stringify() converts a JavaScript Object back to a JSON string.



                  Refer to this link to get more information on how to use XMLHttpRequest



                  EDIT: Referring to another answer posted, people simply use libraries like jQuery AJAX to make asynchronous requests to get data from the web, especially in XML or JSON format.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 16 '18 at 7:54

























                  answered Nov 16 '18 at 7:44









                  Mani Kumar Reddy KancharlaMani Kumar Reddy Kancharla

                  539




                  539






























                      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%2f53332934%2fjavascript-json-requested-object-returns-null%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

                      Bressuire

                      Vorschmack

                      Quarantine