0

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.

2
  • Post your "JSON" file. Or at least the first few lines. Commented Apr 22, 2011 at 20:40
  • So the above file is throwing the SyntaxError(jsonlint.com) bc its valid JSON ? Commented Apr 22, 2011 at 21:01

2 Answers 2

4

A JSON object has to be included in {} or [] at top level, so you cant do

"test1": {...},
"test2": {...}

Use

{
  "test1": {...},
  "test2": {...}
}

instead.

Sign up to request clarification or add additional context in comments.

1 Comment

It's already in the right format, I believe- { "test1": { "testname": alpha, "password": password } }
0

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.