.NET JSON Check, dnjc, is a command line tool to check for JSON
syntax errors using the System.Text.Json parsers.
PS> Get-Content "good.json" | dnjc
# Nothing wrong: no output
PS> Get-Content "bad.json" | dnjc
6 2 '}' is an invalid start of a value.The source code is hosted on GitHub.
dnjc can be installed as a .NET global tool:
dotnet tool install --global DotNetJsonCheck.ToolIf this is the first .NET global tool you've installed, you may need to restart your shell/console for it to pick up the changes to your PATH.
The current versions of the tool and the library that powers it are shown below.
| Package | Version |
|---|---|
| DotNetJsonCheck.Tool | |
| DotNetJsonCheck |
After installing, invoke it, giving it the JSON you want to check on standard input. Using PowerShell, this is something like:
Get-Content "input.json" | dnjcOther shells, like Bash, CMD, and Zsh can use redirection:
dnjc <input.jsonAny errors will be written to standard out.
With no switches, dnjc will allow /* */ and // comments as well
as trailing commas. This JSON document will be successfully parsed:
These defaults were chosen because this is how the
Microsoft.Extensions.Configuration.Json library invokes
JsonDocument.Parse(), and those are the JSON files I'm most often editing.
This behavior can be controlled:
--allow-comments: allows/* */and//style comments (defaults to enabled)--allow-trailing-commas: allows trailing commas in arrays and objects (defaults to enabled)--strict: parses JSON strictly (no comments, no trailing commas)--help: prints help and exits--version: prints version information and exits
Any number of options may be passed. They are processed in order. For
example, to allow comments but not trailing commas, pass --strict --allow-comments in that order.
The Emacs package flycheck-dnjc.el can be used to configure a
Flycheck checker that uses dnjc.
To install it, download it and then install it with
M-x package-install-file RET
/path/to/where/you/downloaded/flycheck-dnjc.el
If you use use-package to manage your packages, add this to
your .emacs file:
(use-package flycheck-dnjc
:config (setup-flycheck-dnjc))If you aren't using any package managers, make sure that
flycheck-dnjc in on your load-path and add:
(require 'flycheck-dnjc)
(setup-flycheck-dnjc)Any errors encountered will be written to standard out, one per line. Each line has the format
LEVEL<TAB>LINE<TAB>COLUMN<TAB>MESSAGE
- LEVEL: the severity of the issue. Currently only "Error" is used.
- LINE: the 1-based line number where the error occurred or started.
- COLUMN: the 0-based byte offset into the line where the error occurred.
- MESSAGE: the detailed error message.
If LINE or COLUMN cannot be determined, they will be empty.
Additional columns may be added in the future at the end. Ensure your parsing can handle this.
The exit code of dnjc will be
- 0: JSON parsed successfully
- 1: invalid arguments
- 2: errors processing the JSON
- 126: catastrophic failure
Things that I want to do, in rough priority order
- Add option to require that the top-level value be an object, an array, &c.
- Use
package-upload-fileto create a hostable package archive?
Copyright 2020, G. Christopher Warrington
dnjc is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License Version 3 as published by the Free Software Foundation.
dnjc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
A copy of the GNU Affero General Public License Version 3 is included in the file LICENSE at the root of the repository.
{ // System.Text.Json can parse this file. "hello": "world", }