A CLI and pre-commit hook for formatting JSON with configurable trailing newline handling.
Project description
format-json
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:
1. format-json vs pretty-format-json
| Feature | format-json | pretty-format-json |
|---|---|---|
| Config option for trailing newline | ✅ | ❌ (Won't implement ever) |
| Preserves all digits of floats | ✅ | ❌ (Unresolved since 2022) |
2. Usage
2.1. As a command-line tool
First, install it:
pip install format-json
Then, in the terminal, you can do something like:
format-json --autofix --no-eof-newline path/to/file.json
All 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.
2.2. As a pre-commit hook
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.)
2.3. Configuration options
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).
3. Instructions for Maintainers
- 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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file format_json-0.1.2.tar.gz.
File metadata
- Download URL: format_json-0.1.2.tar.gz
- Upload date:
- Size: 11.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
65de970bf8ea55886cdcb5cbb95c308823365e408db6597439543f97e08d4251
|
|
| MD5 |
eb48473d324b31490fafd2509f93013a
|
|
| BLAKE2b-256 |
beccb7c951eddf2d3ad491cc8fc10c3bc8d010bd28b3cb74aab7206637d45f10
|
File details
Details for the file format_json-0.1.2-py3-none-any.whl.
File metadata
- Download URL: format_json-0.1.2-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aaa9971bde0e9d9131cdbdc073017526d7f5a87b2131b42cd408850b68e15a75
|
|
| MD5 |
cf8a0e463c2e6537adaaf9fb5e1b2a04
|
|
| BLAKE2b-256 |
0ccafe397032531f633bc998f841ab6ca2d87a009b93a1ff8338ba3c0054be4f
|