Javascript / Json Requested Object returns null
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
|
show 3 more comments
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
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 timeresponse
isnull
orundefined
. 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 givenresponseType = "json"
already before sending the request sorequest.response
is already a javascript object and not a JSON string. So ignore all the code afterrequest.send()
and just useconsole.log(request.response)
insideonload
function which should be declared bold beforerequest.send()
executes.
– Mani Kumar Reddy Kancharla
Nov 16 '18 at 7:12
|
show 3 more comments
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
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
javascript json
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 timeresponse
isnull
orundefined
. 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 givenresponseType = "json"
already before sending the request sorequest.response
is already a javascript object and not a JSON string. So ignore all the code afterrequest.send()
and just useconsole.log(request.response)
insideonload
function which should be declared bold beforerequest.send()
executes.
– Mani Kumar Reddy Kancharla
Nov 16 '18 at 7:12
|
show 3 more comments
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 timeresponse
isnull
orundefined
. 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 givenresponseType = "json"
already before sending the request sorequest.response
is already a javascript object and not a JSON string. So ignore all the code afterrequest.send()
and just useconsole.log(request.response)
insideonload
function which should be declared bold beforerequest.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
|
show 3 more comments
3 Answers
3
active
oldest
votes
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.
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
add a comment |
I used the browser XMLHttpRequest object for Ajax about 12 years ago.
https://api.jquery.com/jquery.getjson/
add a comment |
You have two issues in your code:
- 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 mentionrequest.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 asrequest.response
and you need not parse it. So you could useJSON.parse(request.responseText)
if you want to parse it yourselves or directly userequest.response
as a JavaScript object. - You're trying to store
request.response
into a variable outside the load function which will just provide the value ofrequest.response
at that time which could benull
if the response is not received yet. You need to get the response in theonload
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.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
I used the browser XMLHttpRequest object for Ajax about 12 years ago.
https://api.jquery.com/jquery.getjson/
add a comment |
I used the browser XMLHttpRequest object for Ajax about 12 years ago.
https://api.jquery.com/jquery.getjson/
add a comment |
I used the browser XMLHttpRequest object for Ajax about 12 years ago.
https://api.jquery.com/jquery.getjson/
I used the browser XMLHttpRequest object for Ajax about 12 years ago.
https://api.jquery.com/jquery.getjson/
answered Nov 16 '18 at 7:53
Dudeman3000Dudeman3000
124213
124213
add a comment |
add a comment |
You have two issues in your code:
- 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 mentionrequest.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 asrequest.response
and you need not parse it. So you could useJSON.parse(request.responseText)
if you want to parse it yourselves or directly userequest.response
as a JavaScript object. - You're trying to store
request.response
into a variable outside the load function which will just provide the value ofrequest.response
at that time which could benull
if the response is not received yet. You need to get the response in theonload
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.
add a comment |
You have two issues in your code:
- 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 mentionrequest.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 asrequest.response
and you need not parse it. So you could useJSON.parse(request.responseText)
if you want to parse it yourselves or directly userequest.response
as a JavaScript object. - You're trying to store
request.response
into a variable outside the load function which will just provide the value ofrequest.response
at that time which could benull
if the response is not received yet. You need to get the response in theonload
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.
add a comment |
You have two issues in your code:
- 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 mentionrequest.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 asrequest.response
and you need not parse it. So you could useJSON.parse(request.responseText)
if you want to parse it yourselves or directly userequest.response
as a JavaScript object. - You're trying to store
request.response
into a variable outside the load function which will just provide the value ofrequest.response
at that time which could benull
if the response is not received yet. You need to get the response in theonload
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.
You have two issues in your code:
- 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 mentionrequest.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 asrequest.response
and you need not parse it. So you could useJSON.parse(request.responseText)
if you want to parse it yourselves or directly userequest.response
as a JavaScript object. - You're trying to store
request.response
into a variable outside the load function which will just provide the value ofrequest.response
at that time which could benull
if the response is not received yet. You need to get the response in theonload
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.
edited Nov 16 '18 at 7:54
answered Nov 16 '18 at 7:44
Mani Kumar Reddy KancharlaMani Kumar Reddy Kancharla
539
539
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 timeresponse
isnull
orundefined
. 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 sorequest.response
is already a javascript object and not a JSON string. So ignore all the code afterrequest.send()
and just useconsole.log(request.response)
insideonload
function which should be declared bold beforerequest.send()
executes.– Mani Kumar Reddy Kancharla
Nov 16 '18 at 7:12