Why can't this code access my JSON file in netbeans?
So I am trying to learn Javascript. I created a JSON file called "Ancest.json". Then, in a new file on netbeans I tried to execute this code accessing that file:
var ancestry = JSON.parse(Ancest);
console.log(ancestry.length);
I am getting a rejection saying "Ancest is not defined".
What am I doing wrong? Attached is a screen shot. Thank you for your time.
javascript
add a comment |
So I am trying to learn Javascript. I created a JSON file called "Ancest.json". Then, in a new file on netbeans I tried to execute this code accessing that file:
var ancestry = JSON.parse(Ancest);
console.log(ancestry.length);
I am getting a rejection saying "Ancest is not defined".
What am I doing wrong? Attached is a screen shot. Thank you for your time.
javascript
You need to assign a string ancest or var Ancest = './Ancest.json'; Depending on the location and path of Ancest.json, you may have to put in full path or linked path if the .js file exists in the same path. Also put the path in single quote or double quote. Make sure you have access to read the file. It also needs correct FS permissions.
– Apollo SOFTWARE
Jan 3 at 0:48
add a comment |
So I am trying to learn Javascript. I created a JSON file called "Ancest.json". Then, in a new file on netbeans I tried to execute this code accessing that file:
var ancestry = JSON.parse(Ancest);
console.log(ancestry.length);
I am getting a rejection saying "Ancest is not defined".
What am I doing wrong? Attached is a screen shot. Thank you for your time.
javascript
So I am trying to learn Javascript. I created a JSON file called "Ancest.json". Then, in a new file on netbeans I tried to execute this code accessing that file:
var ancestry = JSON.parse(Ancest);
console.log(ancestry.length);
I am getting a rejection saying "Ancest is not defined".
What am I doing wrong? Attached is a screen shot. Thank you for your time.
javascript
javascript
asked Jan 3 at 0:43
Evan12Evan12
96
96
You need to assign a string ancest or var Ancest = './Ancest.json'; Depending on the location and path of Ancest.json, you may have to put in full path or linked path if the .js file exists in the same path. Also put the path in single quote or double quote. Make sure you have access to read the file. It also needs correct FS permissions.
– Apollo SOFTWARE
Jan 3 at 0:48
add a comment |
You need to assign a string ancest or var Ancest = './Ancest.json'; Depending on the location and path of Ancest.json, you may have to put in full path or linked path if the .js file exists in the same path. Also put the path in single quote or double quote. Make sure you have access to read the file. It also needs correct FS permissions.
– Apollo SOFTWARE
Jan 3 at 0:48
You need to assign a string ancest or var Ancest = './Ancest.json'; Depending on the location and path of Ancest.json, you may have to put in full path or linked path if the .js file exists in the same path. Also put the path in single quote or double quote. Make sure you have access to read the file. It also needs correct FS permissions.
– Apollo SOFTWARE
Jan 3 at 0:48
You need to assign a string ancest or var Ancest = './Ancest.json'; Depending on the location and path of Ancest.json, you may have to put in full path or linked path if the .js file exists in the same path. Also put the path in single quote or double quote. Make sure you have access to read the file. It also needs correct FS permissions.
– Apollo SOFTWARE
Jan 3 at 0:48
add a comment |
2 Answers
2
active
oldest
votes
JSON.parse
method accepts a string which is the JSON object to be parsed into a JavaScript object.
You need to get the content of the file or to move the content of your JSON file into a string variable in the js file. Then you can parse it:
console.log(JSON.parse('{ "a": "test" }'));
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
I got it to work with a simple example but not with a more complex example such : "var ances = JSON.parse([{ "name": "Emma Million", "sex": "f", "born": "monday", "father": "Mike"}, {"name": "Frank", "sex": "m", "born": "tuesday", "father": "Paul"}]); console.log(ances);" Also, are you sure there is no way to enter a file name? The book I am reading uses this example: "var ancestry = JSON.parse(ANCESTRY_FILE); console.log(ancestry.length);" and it says ANCESTRY_FILE is a file. I just feel like there should be a way to enter a file because files might contain a lot of data.
– Evan12
Jan 3 at 1:15
you didn't pass a string toJSON.parse
but a JavaScript array in the code above in the comment. That should be a string, sorround all with''
and use a correct JSON
– quirimmo
Jan 3 at 1:17
as far as i know, and as the documentation I posted you above states, that method takes a JSON object as string. If there is some trick with netbeans, then no idea.
– quirimmo
Jan 3 at 1:19
add a comment |
You need to assign a string Ancest or var Ancest = './Ancest.json'; Depending on the location and path of Ancest.json, you may have to put in the full path or linked path if the .js file exists in the same path. Also put the path in single quote or double quote. Make sure you have access to read the file.
It also needs correct file system permissions. This is all contingent on who this js is running as, delegate permissions on the parent directories, etc, etc. Chances are permissions are okay for read.
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%2f54014999%2fwhy-cant-this-code-access-my-json-file-in-netbeans%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
JSON.parse
method accepts a string which is the JSON object to be parsed into a JavaScript object.
You need to get the content of the file or to move the content of your JSON file into a string variable in the js file. Then you can parse it:
console.log(JSON.parse('{ "a": "test" }'));
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
I got it to work with a simple example but not with a more complex example such : "var ances = JSON.parse([{ "name": "Emma Million", "sex": "f", "born": "monday", "father": "Mike"}, {"name": "Frank", "sex": "m", "born": "tuesday", "father": "Paul"}]); console.log(ances);" Also, are you sure there is no way to enter a file name? The book I am reading uses this example: "var ancestry = JSON.parse(ANCESTRY_FILE); console.log(ancestry.length);" and it says ANCESTRY_FILE is a file. I just feel like there should be a way to enter a file because files might contain a lot of data.
– Evan12
Jan 3 at 1:15
you didn't pass a string toJSON.parse
but a JavaScript array in the code above in the comment. That should be a string, sorround all with''
and use a correct JSON
– quirimmo
Jan 3 at 1:17
as far as i know, and as the documentation I posted you above states, that method takes a JSON object as string. If there is some trick with netbeans, then no idea.
– quirimmo
Jan 3 at 1:19
add a comment |
JSON.parse
method accepts a string which is the JSON object to be parsed into a JavaScript object.
You need to get the content of the file or to move the content of your JSON file into a string variable in the js file. Then you can parse it:
console.log(JSON.parse('{ "a": "test" }'));
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
I got it to work with a simple example but not with a more complex example such : "var ances = JSON.parse([{ "name": "Emma Million", "sex": "f", "born": "monday", "father": "Mike"}, {"name": "Frank", "sex": "m", "born": "tuesday", "father": "Paul"}]); console.log(ances);" Also, are you sure there is no way to enter a file name? The book I am reading uses this example: "var ancestry = JSON.parse(ANCESTRY_FILE); console.log(ancestry.length);" and it says ANCESTRY_FILE is a file. I just feel like there should be a way to enter a file because files might contain a lot of data.
– Evan12
Jan 3 at 1:15
you didn't pass a string toJSON.parse
but a JavaScript array in the code above in the comment. That should be a string, sorround all with''
and use a correct JSON
– quirimmo
Jan 3 at 1:17
as far as i know, and as the documentation I posted you above states, that method takes a JSON object as string. If there is some trick with netbeans, then no idea.
– quirimmo
Jan 3 at 1:19
add a comment |
JSON.parse
method accepts a string which is the JSON object to be parsed into a JavaScript object.
You need to get the content of the file or to move the content of your JSON file into a string variable in the js file. Then you can parse it:
console.log(JSON.parse('{ "a": "test" }'));
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
JSON.parse
method accepts a string which is the JSON object to be parsed into a JavaScript object.
You need to get the content of the file or to move the content of your JSON file into a string variable in the js file. Then you can parse it:
console.log(JSON.parse('{ "a": "test" }'));
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
console.log(JSON.parse('{ "a": "test" }'));
console.log(JSON.parse('{ "a": "test" }'));
answered Jan 3 at 0:47
quirimmoquirimmo
7,71811536
7,71811536
I got it to work with a simple example but not with a more complex example such : "var ances = JSON.parse([{ "name": "Emma Million", "sex": "f", "born": "monday", "father": "Mike"}, {"name": "Frank", "sex": "m", "born": "tuesday", "father": "Paul"}]); console.log(ances);" Also, are you sure there is no way to enter a file name? The book I am reading uses this example: "var ancestry = JSON.parse(ANCESTRY_FILE); console.log(ancestry.length);" and it says ANCESTRY_FILE is a file. I just feel like there should be a way to enter a file because files might contain a lot of data.
– Evan12
Jan 3 at 1:15
you didn't pass a string toJSON.parse
but a JavaScript array in the code above in the comment. That should be a string, sorround all with''
and use a correct JSON
– quirimmo
Jan 3 at 1:17
as far as i know, and as the documentation I posted you above states, that method takes a JSON object as string. If there is some trick with netbeans, then no idea.
– quirimmo
Jan 3 at 1:19
add a comment |
I got it to work with a simple example but not with a more complex example such : "var ances = JSON.parse([{ "name": "Emma Million", "sex": "f", "born": "monday", "father": "Mike"}, {"name": "Frank", "sex": "m", "born": "tuesday", "father": "Paul"}]); console.log(ances);" Also, are you sure there is no way to enter a file name? The book I am reading uses this example: "var ancestry = JSON.parse(ANCESTRY_FILE); console.log(ancestry.length);" and it says ANCESTRY_FILE is a file. I just feel like there should be a way to enter a file because files might contain a lot of data.
– Evan12
Jan 3 at 1:15
you didn't pass a string toJSON.parse
but a JavaScript array in the code above in the comment. That should be a string, sorround all with''
and use a correct JSON
– quirimmo
Jan 3 at 1:17
as far as i know, and as the documentation I posted you above states, that method takes a JSON object as string. If there is some trick with netbeans, then no idea.
– quirimmo
Jan 3 at 1:19
I got it to work with a simple example but not with a more complex example such : "var ances = JSON.parse([{ "name": "Emma Million", "sex": "f", "born": "monday", "father": "Mike"}, {"name": "Frank", "sex": "m", "born": "tuesday", "father": "Paul"}]); console.log(ances);" Also, are you sure there is no way to enter a file name? The book I am reading uses this example: "var ancestry = JSON.parse(ANCESTRY_FILE); console.log(ancestry.length);" and it says ANCESTRY_FILE is a file. I just feel like there should be a way to enter a file because files might contain a lot of data.
– Evan12
Jan 3 at 1:15
I got it to work with a simple example but not with a more complex example such : "var ances = JSON.parse([{ "name": "Emma Million", "sex": "f", "born": "monday", "father": "Mike"}, {"name": "Frank", "sex": "m", "born": "tuesday", "father": "Paul"}]); console.log(ances);" Also, are you sure there is no way to enter a file name? The book I am reading uses this example: "var ancestry = JSON.parse(ANCESTRY_FILE); console.log(ancestry.length);" and it says ANCESTRY_FILE is a file. I just feel like there should be a way to enter a file because files might contain a lot of data.
– Evan12
Jan 3 at 1:15
you didn't pass a string to
JSON.parse
but a JavaScript array in the code above in the comment. That should be a string, sorround all with ''
and use a correct JSON– quirimmo
Jan 3 at 1:17
you didn't pass a string to
JSON.parse
but a JavaScript array in the code above in the comment. That should be a string, sorround all with ''
and use a correct JSON– quirimmo
Jan 3 at 1:17
as far as i know, and as the documentation I posted you above states, that method takes a JSON object as string. If there is some trick with netbeans, then no idea.
– quirimmo
Jan 3 at 1:19
as far as i know, and as the documentation I posted you above states, that method takes a JSON object as string. If there is some trick with netbeans, then no idea.
– quirimmo
Jan 3 at 1:19
add a comment |
You need to assign a string Ancest or var Ancest = './Ancest.json'; Depending on the location and path of Ancest.json, you may have to put in the full path or linked path if the .js file exists in the same path. Also put the path in single quote or double quote. Make sure you have access to read the file.
It also needs correct file system permissions. This is all contingent on who this js is running as, delegate permissions on the parent directories, etc, etc. Chances are permissions are okay for read.
add a comment |
You need to assign a string Ancest or var Ancest = './Ancest.json'; Depending on the location and path of Ancest.json, you may have to put in the full path or linked path if the .js file exists in the same path. Also put the path in single quote or double quote. Make sure you have access to read the file.
It also needs correct file system permissions. This is all contingent on who this js is running as, delegate permissions on the parent directories, etc, etc. Chances are permissions are okay for read.
add a comment |
You need to assign a string Ancest or var Ancest = './Ancest.json'; Depending on the location and path of Ancest.json, you may have to put in the full path or linked path if the .js file exists in the same path. Also put the path in single quote or double quote. Make sure you have access to read the file.
It also needs correct file system permissions. This is all contingent on who this js is running as, delegate permissions on the parent directories, etc, etc. Chances are permissions are okay for read.
You need to assign a string Ancest or var Ancest = './Ancest.json'; Depending on the location and path of Ancest.json, you may have to put in the full path or linked path if the .js file exists in the same path. Also put the path in single quote or double quote. Make sure you have access to read the file.
It also needs correct file system permissions. This is all contingent on who this js is running as, delegate permissions on the parent directories, etc, etc. Chances are permissions are okay for read.
answered Jan 3 at 0:51


Apollo SOFTWAREApollo SOFTWARE
10.1k43859
10.1k43859
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%2f54014999%2fwhy-cant-this-code-access-my-json-file-in-netbeans%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
You need to assign a string ancest or var Ancest = './Ancest.json'; Depending on the location and path of Ancest.json, you may have to put in full path or linked path if the .js file exists in the same path. Also put the path in single quote or double quote. Make sure you have access to read the file. It also needs correct FS permissions.
– Apollo SOFTWARE
Jan 3 at 0:48