Skip to main content

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-ascii preserve 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.dev to install developer dependencies.
  • Run pytest to execute the automated tests replicated from the upstream project.
  • Use tox to exercise the full test matrix, linting, and formatting targets.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

format_json-0.1.2.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

format_json-0.1.2-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

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

Hashes for format_json-0.1.2.tar.gz
Algorithm Hash digest
SHA256 65de970bf8ea55886cdcb5cbb95c308823365e408db6597439543f97e08d4251
MD5 eb48473d324b31490fafd2509f93013a
BLAKE2b-256 beccb7c951eddf2d3ad491cc8fc10c3bc8d010bd28b3cb74aab7206637d45f10

See more details on using hashes here.

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

Hashes for format_json-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 aaa9971bde0e9d9131cdbdc073017526d7f5a87b2131b42cd408850b68e15a75
MD5 cf8a0e463c2e6537adaaf9fb5e1b2a04
BLAKE2b-256 0ccafe397032531f633bc998f841ab6ca2d87a009b93a1ff8338ba3c0054be4f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page