All Projects → prantlf → jsonlint

prantlf / jsonlint

Licence: MIT license
JSON/CJSON/JSON5 parser, syntax & schema validator and pretty-printer with a command-line client, written in pure JavaScript.

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects
PEG.js
56 projects
Yacc
648 projects
typescript
32286 projects
Nearley
35 projects
Lex
420 projects

Projects that are alternatives of or similar to jsonlint

dockerfile-utils
A library and command line interface for formatting and linting Dockerfiles.
Stars: ✭ 17 (-19.05%)
Mutual labels:  lint, validation
openapi-lint-vscode
OpenAPI 2.0/3.0.x intellisense, validator, linter, converter and resolver extension for Visual Studio Code
Stars: ✭ 47 (+123.81%)
Mutual labels:  lint, validation
lint-deps
Lint for unused or missing dependencies in your node.js projects. Customize with plugins or configuration.
Stars: ✭ 48 (+128.57%)
Mutual labels:  lint
HTML-Lint
A code quality bookmarklet and command-line tool
Stars: ✭ 20 (-4.76%)
Mutual labels:  lint
folderslint
📁 Directory structure linter for JavaScript projects
Stars: ✭ 131 (+523.81%)
Mutual labels:  lint
litho-lint-rules
Lint rules for Litho by Facebook http://fblitho.com/
Stars: ✭ 14 (-33.33%)
Mutual labels:  lint
arcanist-linters
A collection of custom Arcanist linters
Stars: ✭ 64 (+204.76%)
Mutual labels:  lint
shell-ci-build
A submodule to lint your shell projects with shellcheck in travis.ci builds
Stars: ✭ 39 (+85.71%)
Mutual labels:  lint
prettier-check
Check that all files match prettier code style.
Stars: ✭ 54 (+157.14%)
Mutual labels:  lint
sack.vfs
Node addon which adds a virtual file system interface; websockets; json(6) parsing; sql support(sqlite,odbc); javascript sched_yield; ssl certificate generation; more...
Stars: ✭ 29 (+38.1%)
Mutual labels:  json5
gavel-spec
Behavior specification for Gavel, validator of HTTP transactions
Stars: ✭ 105 (+400%)
Mutual labels:  validation
breakcheck
Backwards compatibility linter for Go.
Stars: ✭ 66 (+214.29%)
Mutual labels:  lint
json-viewer
Pretty JSON viewer for the terminal
Stars: ✭ 26 (+23.81%)
Mutual labels:  pretty-print
lint
Linting and formatting for SAS® code
Stars: ✭ 16 (-23.81%)
Mutual labels:  lint
puppet-lint-security-plugins
Identify security issues in your Puppet code.
Stars: ✭ 20 (-4.76%)
Mutual labels:  lint
validation
Aplus Framework Validation Library
Stars: ✭ 99 (+371.43%)
Mutual labels:  validation
chart-testing-action
A GitHub Action to lint and test Helm charts
Stars: ✭ 139 (+561.9%)
Mutual labels:  lint
KaiZen-OpenApi-Parser
High-performance Parser, Validator, and Java Object Model for OpenAPI 3.x
Stars: ✭ 119 (+466.67%)
Mutual labels:  lint
relint
General purpose RegEx based file linter.
Stars: ✭ 33 (+57.14%)
Mutual labels:  lint
arduino-lint
Tool to check for problems with Arduino projects
Stars: ✭ 63 (+200%)
Mutual labels:  lint

JSON Lint

Latest version Dependency status Code coverage

A JSON/CJSON/JSON5 parser, validator and pretty-printer with a command-line client. See it in action at https://prantlf.github.io/jsonlint/.

This is a fork of the original project (zaach/jsonlint) with the following enhancements:

  • Handles multiple files on the command line (by Greg Inman).
  • Walks directories recursively (by Paul Vollmer).
  • Provides 100% compatible interface to the native JSON.parse method.
  • Optionally recognizes JavaScript-style comments (CJSON) and single quoted strings (JSON5).
  • Optionally ignores trailing commas and reports duplicate object keys as an error.
  • Optionally checks that also the expected format matches, including sorted object keys.
  • Supports JSON Schema drafts 04, 06 and 07.
  • Offers pretty-printing including comment-stripping and object keys without quotes (JSON5).
  • Prefers the native JSON parser if possible to run 7x faster than the custom parser.
  • Reports errors with rich additional information. From the JSON Schema validation too.
  • Consumes configuration from both command line and configuration files.
  • Implements JavaScript modules using UMD to work in Node.js, in a browser, everywhere.
  • Depends on up-to-date npm modules with no installation warnings.
  • Small size - 18.7 kB minified, 6.54 kB gzipped, 5.16 kB brotlied.

Note: In comparison with the original project, this package exports only the parse method; not the Parser object.

Integration to the favourite task loaders for JSON file validation is provided by the following NPM modules:

Synopsis

Check syntax of JSON files:

jsonlint -q data/*.json

Parse a JSON string:

const { parse } = require('@prantlf/jsonlint')
const data = parse('{"creative": false}')

Example of an error message:

Parse error on line 1, column 14:
{"creative": ?}
-------------^
Unexpected token "?"

Command-line Interface

Install jsonlint with npm, pnpm or yarn globally to be able to use the command-line interface in any directory:

npm i -g @prantlf/jsonlint
pnpm i -g @prantlf/jsonlint
yarn add --global @prantlf/jsonlint

Validate a single file:

jsonlint myfile.json

or pipe the JSON input into stdin:

cat myfile.json | jsonlint

or process all .json files in a directory and rewriting them with the pretty-printed output:

jsonlint --in-place --pretty-print mydir

By default, jsonlint will either report a syntax error with details or pretty-print the source if it is valid.

A more complex example: check all JSON files in a Node.js project, except for dependencies in node_modules, allow comments (CJSON) and trailing commas, forbid duplicated object keys, print processed files names on the console, print errors on a single line and if an error occurs, continue with other files:

jsonlint --comments --trailing-commas --no-duplicate-keys \
  --log-files --compact --continue '**/*.json' '!**/node_modules'

The same parameters can be passed from a configuration file:

{
  "comments": true,
  "trailing-commas": true,
  "duplicate-keys": false,
  "log-files": true,
  "compact": true,
  "continue": true,
  "patterns": ["**/*.json", "!**/node_modules"]
}

The input can be checked not only to be a valid JSON, but also to be formatted according to the coding standard. For example, check that there is a trailing li break in each JSON file, in addition to alphabetically sorted keys and no duplicate keys:

$ jsonlint -ksDr *.json

File: package.json
Formatted output differs
===================================================================
--- package.json.orig
+++ package.json
@@ -105,4 +105,4 @@
    "lint",
    "jsonlint"
  ]
-}
+}
\ No newline at end of file

Usage

Usage: jsonlint [options] [<file, directory, pattern> ...]

Options

-f, --config [file]          read options from a custom configuration file
-F, --no-config              disable searching for configuration file
-s, --sort-keys              sort object keys (not when prettifying)
-E, --extensions [ext]       file extensions to process for directory walk
                              (default: ["json","JSON"])
-i, --in-place               overwrite the input files
-j, --diff                   print difference instead of writing the output
-k, --check                  check that the input is equal to the output
-t, --indent [num|char]      number of spaces or specific characters
                              to use for indentation (default: 2)
-c, --compact                compact error display
-M, --mode [mode]            set other parsing flags according to a format
                              type (default: "json")
-C, --comments               recognize and ignore JavaScript-style comments
-S, --single-quoted-strings  support single quotes as string delimiters
-T, --trailing-commas        ignore trailing commas in objects and arrays
-D, --no-duplicate-keys      report duplicate object keys as an error
-V, --validate [file]        JSON schema file to use for validation
-e, --environment [env]      which specification of JSON Schema the
                              validation file uses
-x, --context [num]          line count used as the diff context (default: 3)
-l, --log-files              print only the parsed file names to stdout
-q, --quiet                  do not print the parsed json to stdout
-n, --continue               continue with other files if an error occurs
-p, --pretty-print           prettify the input instead of stringifying
                              the parsed object
-P, --pretty-print-invalid   force pretty-printing even for invalid input
-r, --trailing-newline       ensure a line break at the end of the output
-R, --no-trailing-newline    ensure no line break at the end of the output
--prune-comments             omit comments from the prettified output
--strip-object-keys          strip quotes from object keys if possible
                              (JSON5)
--enforce-double-quotes      surrounds all strings with double quotes
--enforce-single-quotes      surrounds all strings with single quotes
                              (JSON5)
--trim-trailing-commas       omit trailing commas from objects and arrays
                              (JSON5)
-v, --version                output the version number
-h, --help                   output usage information

You can use BASH patterns for including and excluding files (only files). Patterns are case-sensitive and have to use slashes as a path separators. A pattern to exclude from processing starts with "!".

Parsing mode can be "cjson" or "json5" to enable other flags automatically. If no files or directories are specified, stdin will be parsed. Environments for JSON schema validation are "json-schema-draft-04", "json-schema-draft-06" or "json-schema-draft-07". If not specified, it will be auto-detected.

Configuration

In addition to the command line parameters, the options can be supplied from the following files:

package.json, key jsonlint
.jsonlintrc
.jsonlintrc.json
.jsonlintrc.yaml
.jsonlintrc.yml
.jsonlintrc.js
.jsonlintrc.cjs
jsonlint.config.js
jsonlint.config.cjs

The automatic search for one of the following locations above can be disabled by the command-line parameter -F|--no-config. A concrete configuration file can be specified by the command-line parameter -f|--config [file]. Parameters from the command line will have higher priority than parameters from a configuration file.

The configuration is an object with the following properties, described above, which can be entered either in the kebab-case or in the camel-case:

Parameter Alias
patterns
sort-keys sortKeys
extensions
in-place inPlace
diff
check
indent
compact
mode
comments
single-quoted-strings singleQuotedStrings
trailing-commas trailingCommas
duplicate-keys duplicateKeys
validate
environment
log-files logFiles
quiet
continue
pretty-print prettyPrint
pretty-print-invalid prettyPrintInvalid
trailing-newline trailingNewline'
prune-comments pruneComments
strip-object-keys stripObjectKeys
enforce-double-quotes enforceDoubleQuotes
enforce-single-quotes enforceSingleQuotes
trim-trailing-commas trimTrailingCommas

The parameter config will be ignored in configuration files. The extra parameter patterns can be set to an array of strings with paths or patterns instead of putting them to the command line.

Module Interface

Install jsonlint with npm locally to be able to use the module programmatically:

npm i @prantlf/jsonlint -S

The only exported item is the parse method, which parses a string in the JSON format to a JavaScript object, array, or value:

const { parse } = require('@prantlf/jsonlint')
// Fails at the position of the character "?".
const data2 = parse('{"creative": ?}') // throws an error
// Succeeds returning the parsed JSON object.
const data3 = parse('{"creative": false}')
// Recognizes comments and single-quoted strings.
const data3 = parse("{'creative': true /* for creativity */}", {
  ignoreComments: true,
  allowSingleQuotedStrings: true
})

Have a look at the source of the on-line page to see how to use jsonlint on web page.

The exported parse method is compatible with the native JSON.parse method. The second parameter provides the additional functionality:

parse(input, [reviver|options])
Parameter Description
input text in the JSON format (string)
reviver converts object and array values (function)
options customize parsing options (object)

The parse method offers more detailed error information, than the native JSON.parse method and it supports additional parsing options:

Option Description
ignoreComments ignores single-line and multi-line JavaScript-style comments during parsing as another "whitespace" (boolean)
ignoreTrailingCommas ignores trailing commas in objects and arrays (boolean)
allowSingleQuotedStrings accepts strings delimited by single-quotes too (boolean)
allowDuplicateObjectKeys allows reporting duplicate object keys as an error (boolean)
mode sets multiple options according to the type of input data (string)
reviver converts object and array values (function)

The mode parameter (string) sets parsing options to match a common format of input data:

Mode Description
json complies to the pure standard JSON (default if not set)
cjson JSON with comments (sets ignoreComments)
json5 complies to JSON5 (sets ignoreComments, allowSingleQuotedStrings, ignoreTrailingCommas and enables other JSON5 features)

Schema Validation

You can validate the input against a JSON schema using the lib/validator module. The validate method accepts either an earlier parsed JSON data or a string with the JSON input:

const { compile } = require('@prantlf/jsonlint/lib/validator')
const validate = compile('string with JSON schema')
// Throws an error in case of failure.
const parsed = validate('string with JSON data')

If a string is passed to the validate method, the same options as for parsing JSON data can be passed as the second parameter. Compiling JSON schema supports the same options as parsing JSON data too (except for reviver). They can be passed as the second (object) parameter. The optional second environment parameter can be passed either as a string or as an additional property in the options object too:

const validate = compile('string with JSON schema', {
  environment: 'json-schema-draft-04'
})

Pretty-Printing

You can parse a JSON string to an array of tokens and print it back to a string with some changes applied. It can be unification of whitespace, reformatting or stripping comments, for example. (Raw token values must be enabled when tokenizing the JSON input.)

const { tokenize } = require('@prantlf/jsonlint')
const tokens = tokenize('string with JSON data', { rawTokens: true })
const { print } = require('@prantlf/jsonlint/lib/printer')
const output = print(tokens, { indent: 2 })

The tokenize method accepts options in the second optional parameter. See the tokenize method above for more information.

The print method accepts an object options as the second optional parameter. The following properties will be recognized there:

Option Description
indent count of spaces or the specific characters to be used as an indentation unit
pruneComments will omit all tokens with comments
stripObjectKeys will not print quotes around object keys which are JavaScript identifier names
enforceDoubleQuotes will surround all strings with double quotes
enforceSingleQuotes will surround all strings with single quotes
trimTrailingCommas will omit all trailing commas after the last object entry or array item
// Just concatenate the tokens to produce the same output as was the input.
print(tokens)
// Strip all whitespace. (Just like `JSON.stringify(json)` would do it,
// but leaving comments in the output.)
print(tokens, {})
// Print to multiple lines without object and array indentation.
// (Just introduce line breaks.)
print(tokens, { indent: '' })
// Print to multiple lines with object and array indentation. (Just like
//`JSON.stringify(json, undefined, 2)` would do it, but retaining comments.)
print(tokens, { indent: 2 })
// Print to multiple lines with object and array indentation, omit comments.
// (Just like `JSON.stringify(json, undefined, '  ')` would do it.)
print(tokens, { indent: '  ', pruneComments: true })
// Print to multiple lines with indentation enabled and JSON5 object keys.
print(tokens, { indent: '\t', stripObjectKeys: true })
// Print to multiple lines with indentation enabled, unify JSON5 formatting.
print(tokens, {
  indent: '    ',
  enforceDoubleQuotes: true,
  trimTrailingCommas: true
})

Tokenizing

The method tokenize has the same prototype as the method parse, but returns an array of tokens instead of the JSON object.

const { tokenize } = require('@prantlf/jsonlint')
const tokens = tokenize('{"flag":true /* default */}', {
  ignoreComments: true,
  rawTokens: true
}))
// Returns the following array:
// [
//   { type: 'symbol',     raw: '{',      value: '{' },
//   { type: 'literal',    raw: '"flag"', value: 'flag' },
//   { type: 'symbol',     raw: ':',      value: ':' },
//   { type: 'literal',    raw: 'true',   value: true },
//   { type: 'whitespace', raw: ' ' },
//   { type: 'comment',    raw: '/* default */' },
//   { type: 'symbol',     raw: '}',      value: '}' }
// ]

The tokenize method accepts options in the second optional parameter. See the parse method above for the shared options. There are several additional options supported for the tokenization:

Option Description
rawTokens adds a raw property with the original string from the JSON input
tokenLocations adds a location property with start, end and length of the original string from the JSON input
tokenPaths adds a path property with an array of keys and array indexes "on the way to" the token's value

If you want to retain comments or whitespace for pretty-printing, for example, set rawTokens to true. (The print method requires tokens produced with this flag enabled.)

Performance

This is a part of an output from the parser benchmark, when parsing a 4.2 KB formatted string (package.json) with Node.js 12.14.0:

jsonlint using native JSON.parse x 97,109 ops/sec ±0.81% (93 runs sampled)
jsonlint using hand-coded parser x 7,256 ops/sec ±0.54% (90 runs sampled)
jsonlint using tokenising parser x 6,387 ops/sec ±0.44% (88 runs sampled)

A custom JSON parser is a lot slower than the built-in one. However, it is more important to have a clear error reporting than the highest speed in scenarios like parsing configuration files. (For better error-reporting, the speed can be preserved by using the native parser initially and re-parsing with another parser only in case of failure.) Features like comments or JSON5 are also helpful in configuration files. Tokens preserve the complete input and can be used for pretty-printing without losing the comments.

Error Handling

If parsing fails, a SyntaxError will be thrown with the following properties:

Property Description
message the full multi-line error message
reason one-line explanation of the error
excerpt part of the input string around the error
pointer "--^" pointing to the error in excerpt
location object pointing to the error location

The location object contains properties line, column and offset.

The following code logs twice the following message:

Parse error on line 1, column 14:
{"creative": ?}
-------------^
Unexpected token "?"
const { parse } = require('@prantlf/jsonlint')
try {
  parse('{"creative": ?}')
} catch (error) {
  const { message, reason, excerpt, pointer, location } = error
  const { column, line, offset } = location.start
  // Logs the complete error message:
  console.log(message)
  // Logs the same text as included in the `message` property:
  console.log(`Parse error on line ${line}, ${column} column:
${excerpt}
${pointer}
${reason}`)
}

License

Copyright (C) 2012-2022 Zachary Carter, Ferdinand Prantl

Licensed under the MIT License.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].