-3

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.

Snowmanzzz
  • 240
  • 5
  • 15

3 Answers3

2

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.

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153
1

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).

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
0

I find a super simple one, just use node, then require this json file.

Snowmanzzz
  • 240
  • 5
  • 15