That's it, i'm a careless guy, i always miss something or what if i'm writing a json. I think maybe we can utilize irb.
-
Try checking out this answer from another question: http://stackoverflow.com/a/3710506 – Kravock Nov 28 '14 at 02:41
3 Answers
Before looking at Node, look at your editor. Does it do plugins (say, Sublime Text)? If so, install a JSON linter/validator that won't let you save until you've fixed the errors. Problem solved.
No such luck? Look into using grunt or gulp with a simple JSON validator task (of the kind "look for **/*.json, check that"). e.g. https://www.npmjs.org/package/grunt-jsonlint or https://www.npmjs.org/package/gulp-jsonlint ...
Or, even just use plain old https://www.npmjs.org/package/jsonlint on its own to check individual files.
This is a solved problem, pick your favourite solution.

- 49,297
- 16
- 112
- 153
You could always create an alias:
alias jsonlint="echo \"try{JSON.parse(require('fs').readFileSync(process.argv[2])); process.exit(0);}catch(e){process.exit(1);}\" | node"
and use it like:
jsonlint some_file.json
or if you don't want the error output
jsonlint some_file.json 2>/dev/null
In spite of what Snowmanzzz said, require('some_json.json')
isn't guaranteed to detect the file as JSON (especially if it doesn't have the .json
extension).

- 14,451
- 16
- 82
- 145