format-json is a JSON formatter that ships both a standalone CLI and a
pre-commit hook. It is adapted from the
pretty-format-json
pre-commit hook, with only one difference, as seen below.
Table of Contents
| Feature | format-json | pretty-format-json |
|---|---|---|
| Config option for trailing newline | ✅ | ❌ (Won't implement ever) |
| Preserves all digits of floats | ✅ | ❌ (Unresolved since 2022) |
First, install it:
pip install format-jsonThen, in the terminal, you can do something like:
format-json --autofix --no-eof-newline path/to/file.jsonAll command-line options from pretty-format-json are preserved, with the new
--no-eof-newline flag layered on top. See
Section 2.3 for the complete set of config
options.
Add the hook to your .pre-commit-config.yaml:
- repo: https://github.com/jsh9/format-json
rev: <LATEST_VERSION>
hooks:
- id: format-json
args: [--autofix, --no-eof-newline](You can choose your own args.)
format-json accepts the same options whether invoked via the CLI or
pre-commit. Combine them as needed for your workflow:
--autofix- automatically format json files--indent ...- Control the indentation (either a number for a number of spaces or a string of whitespace). Defaults to 2 spaces.--no-ensure-asciipreserve unicode characters instead of converting to escape sequences--no-sort-keys- when autofixing, retain the original key ordering (instead of sorting the keys)--top-keys comma,separated,keys- Keys to keep at the top of mappings.--no-eof-newline: omit the trailing newline (format-json only).
- Run
pip install -e .to install this project in the "editable" mode. - Run
pip install -r requirements.devto install developer dependencies. - Run
pytestto execute the automated tests replicated from the upstream project. - Use
toxto exercise the full test matrix, linting, and formatting targets.