Reputation: 2504
I'm looking to use a JSON file in a Node.js project, but it doesn't seem to be working-
var JsonPath = '../../folderOfjsonFiles';
var JsonFile = JsonPath + 'test.json';
var parseThis = JSON.parse(JsonFile);
console.dir(parseThis);
Any suggestions as to what I'm doing wrong? Running this yields this error:
"test1": {
^
uncaught: SyntaxError: Unexpected token :
at Module._compile (module.js:399:25)
at Object..js (module.js:410:10)
at Module.load (module.js:336:31)
at Function._load (module.js:297:12)
at require (module.js:348:19)
Where test1 is the first JSON object in my file.
This is my JSON file-
{
"test1": {
"testname": "alpha",
"password": "password"
}
}
Even before the JSON parsing, how do I read from a file that I will store locally on the server-side? I feel like I'm overcomplicating this.
Upvotes: 0
Views: 749
Reputation: 624
I store my Express server config in a file and read it like this:
var fs = require('fs');
var path = require('path');
var conf = fs.readFileSync(path.join(__dirname, './config.json'), 'utf8');
Upvotes: 0
Reputation: 3181
A JSON object has to be included in {}
or []
at top level, so you cant do
"test1": {...},
"test2": {...}
Use
{
"test1": {...},
"test2": {...}
}
instead.
Upvotes: 4