6

I have the following JSON:

{
   "request" : {
      "language" : "en",
      "title" : "placeholder",
      "year" : "2014"
   }
}

which I'm trying to parse using the following code:

var json = require('../filename);

Oddly, I receive the SyntaxError

/home/username/code/filename:2
   "request" : {
             ^
SyntaxError: Unexpected token :
(...)

The JSON is perfectly valid according to JSONLint. Am I missing something very obvious?

haroba
  • 2,120
  • 4
  • 22
  • 37

4 Answers4

9

It sounds like you made a .js file, not a .json file.
Therefore, it's being parsed as Javascript, not JSON.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Ah, didn't think of that. My files don't actually have a filename extension. Is there any way to force require() to treat the files as JSON files? Requiring me to use .json extensions seems contrary to usual practice in the UNIX world. – haroba Jan 29 '14 at 01:45
  • @Aqwis: The documentation only says *"`.js` files are interpreted as JavaScript text files, and `.json` files are parsed as JSON text files"* – Felix Kling Jan 29 '14 at 01:47
  • @Aqwis Excepting that requirejs could - but it does *not* - try to guess at the contents of the file, it doesn't have much else to go on .. – user2864740 Jan 29 '14 at 01:47
  • Well, it has the fact that the file starts with a { (which a .js file would never do) to go on. But I guess the answer is no, then. – haroba Jan 29 '14 at 01:48
  • 1
    @Aqwis: No. `{ var foo = '42'; }` is valid JavaScript. `{...}` denotes a block. Of course it's very *uncommon* but the fact that a file starts with `{` is not enough to treat it like JSON. – Felix Kling Jan 29 '14 at 01:49
  • Okay, one point for you. I guess I'll just have to rename all my JSON files. Still would have been nice to be able to specify using an argument whether the file I'm requiring is a JSON or a js file. – haroba Jan 29 '14 at 01:50
9

require() should not be used for loading JSON files. It is used to load node.js modules only, not data. Loading data depending on an extension was effectively deprecated (see countless discussions about require.extensions).

So the right way to load JSON would be something like this:

JSON.parse(require('fs').readFileSync(__dirname + '/filename', 'utf8'))

alex
  • 11,935
  • 3
  • 30
  • 42
1

Bad practice or not, this will failure will also occur on windows if the extension of the file is 'JSON', not 'json'. Just cost me a hour to discover that

mark d drake
  • 1,280
  • 12
  • 20
0

(Edited) Thanks @Aqwis for noticing me

To parse a JSON better use JSON.parse like these thread shows.

Community
  • 1
  • 1
matagus
  • 6,136
  • 2
  • 26
  • 39
  • 3
    No, require has been able to parse JSON since 2011. – haroba Jan 29 '14 at 01:46
  • 1
    From the documentation you linked to: *"`.js` files are interpreted as JavaScript text files, and `.json` files are parsed as JSON text files"*. Next time, if you already link to some documentation, make sure you read it thoroughly beforehand ;) – Felix Kling Jan 29 '14 at 01:48