dnjc, .NET JSON Check

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

Install

dnjc can be installed as a .NET global tool:

dotnet tool install --global DotNetJsonCheck.Tool

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

Versions

The current versions of the tool and the library that powers it are shown below.

PackageVersion
DotNetJsonCheck.ToolDotNetJsonCheck.Tool NuGet version
DotNetJsonCheckDotNetJsonCheck NuGet version

Invocation

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" | dnjc

Other shells, like Bash, CMD, and Zsh can use redirection:

dnjc <input.json

Any errors will be written to standard out.

Options

With no switches, dnjc will allow /* */ and // comments as well as trailing commas. This JSON document will be successfully parsed:

{
    // System.Text.Json can parse this file.
    "hello": "world",
}

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:

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.

Emacs integration

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)

Error reporting

Any errors encountered will be written to standard out, one per line. Each line has the format

LEVEL<TAB>LINE<TAB>COLUMN<TAB>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

To Do

Things that I want to do, in rough priority order

  1. Add option to require that the top-level value be an object, an array, &c.
  2. Use package-upload-file to create a hostable package archive?

License

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.