Skip to main content

JSON Reflow

PyPI - Python Version PyPI - Wheel PyPI - Version GitHub Actions Workflow Status PyPI - License

Python library and CLI tool to reflow JSON files and streams, to allow a better compromise between

  • compactness: try to fit short arrays and objects on a single line, within a given line length limit
  • human readability: multilevel indentation for larger constructs otherwise.

The problem

Standard JSON serialization tools typically only provide two approaches:

  • put everything on a single line: the most compact, but very poor for human readability

  • spread out each and every array item and object property on its own line with appropriate indentation to visualize the structure. This is easier for humans to parse visually (which is why it is often referred to as "prettifying" or "beautifying"), but for larger documents, this easily becomes unwieldy, "too vertical" and very space-inefficient because of all the repeated indentation.

If you want a solution in between, JSON Reflow can help you.

Example

Consider the following example of a (Geo)JSON document.

Compact

In compact form, you have one line, 83 characters in total, no whitespace:

{"type":"Polygon","coordinates":[[[11.4,46.5],[10.3,55.3],[1.1,49.9],[11.4,46.5]]]}

"Pretty"

In the so-called "pretty" form, using 2 space indentation (a common choice), you have 23 lines, 233 characters, of which more than half are the spaces for indentation:

{
  "type": "Polygon",
  "coordinates": [
    [
      [
        11.4,
        46.5
      ],
      [
        10.3,
        55.3
      ],
      [
        1.1,
        49.9
      ],
      [
        11.4,
        46.5
      ]
    ]
  ]
}

JSON Reflow

JSON Reflow allows one to find a better compromise: only spread out arrays or objects over multiple lines if the single-line approach would exceed a given line length.

With the default settings (e.g. 80 characters line length limit, 2 spaces indentation), JSON Reflow produces the following. Just 4 lines, 99 characters in total, and only 4 spaces spent on indentation:

{
  "type": "Polygon",
  "coordinates": [[[11.4, 46.5], [10.3, 55.3], [1.1, 49.9], [11.4, 46.5]]]
}

Taking a shorter line length limit of 40 characters causes a different reflow of the "coordinates" array, resulting in 11 lines, 137 characters in total, and 38 spaces for indentation:

{
  "type": "Polygon",
  "coordinates": [
    [
      [11.4, 46.5],
      [10.3, 55.3],
      [1.1, 49.9],
      [11.4, 46.5]
    ]
  ]
}

Installation

In a virtual environment

JSON Reflow is a Python package and can be installed from PyPI using pip (or something compatible like uv) into your Python environment:

pip install jsonreflow

Standalone command line tool

If you're just interested in using the command line tool, you probably want to install it in a more standalone way, e.g. using pipx:

pipx install jsonreflow

Or uv tool:

uv tool install jsonreflow

If your PATH is set up correctly according to the instructions of pipx or uv, you should then be able to run the jsonreflow command, e.g.:

jsonreflow --help

No-install usage

With uvx (alias for uv tool run), you can also run the tool without (explicitly) installing it,

uvx jsonreflow --help

Command line usage

The jsonreflow command line tool has built-in help under the --help option:

jsonreflow --help

Basics

The most basic usage is just passing a JSON file path, or reading from standard input if no file path is given:

# Provide a file path as argument,
jsonreflow data.json

# Or read from standard input
cat data.json | jsonreflow

This will reflow the JSON document with the default settings of an 80 characters line length limit and 2 spaces indentation.

Line length limit and indentation can be customized:

jsonreflow --max-width 40 --indent 4 data.json

"Assume formatted" mode

By default, JSON Reflow parses the input JSON and re-encodes it before reflowing, to ensure that proper indentation is present for the reflowing logic to work correctly. If you have data that is already properly JSON formatted with consistent indentation, you can use the --assume-formatted option to skip the parsing and re-encoding step, and directly reflow the input as-is.

This has some advantages:

  • no effort is spent on parsing and re-encoding
  • original data encoding is preserved. This avoids subtle, undesired data manipulation introduced by the roundtrip of parsing and re-encoding (e.g. loss of decimal places in floats, or handling of unicode data).
  • The document can be reflowed in a streaming fashion, line per line, without need to keep the entire document in memory.

[!NOTE] This operation mode of JSON Reflow can also be used on data that is not (fully) valid JSON, but close enough in terms of structure and indentation. To be used at one's own risk of course.

In-place file modification

Because of how redirection works in most shells, naively trying to reflow a file in-place with something like jsonreflow data.json > data.json will not work (the redirection will truncate the input file before jsonreflow can read it). Instead, use the --inplace option:

jsonreflow --inplace data.json

Design and implementation

  • No required dependencies outside of the Python standard library
  • The core reflow logic only manipulates whitespace, there is no requirement to parse the JSON and re-serialize it, which allows to preserve the original encoding and avoids subtle data manipulation that can be introduced by parsing and re-encoding
  • The core reflow logic works in a streaming fashion (iterable input, generator output), so large JSON documents can be reflowed without loading the entire document into memory.

References and related tools

Comparable tools:

  • lydell/json-stringify-pretty-compact: JavaScript implementation. Provides an online demo
  • yairlenga/jsonfold aims to have implementations in multiple languages (including Python and JavaScript). Fun fact: JSON Reflow was originally named "jsonfold", and while the initial implementation of JSON Reflow predates yairlenga/jsonfold (by just a couple of months) the latter was actually first published on PyPI, so I had to rename my package to "JSON Reflow".

Download files

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

Source Distribution

jsonreflow-0.8.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

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

jsonreflow-0.8.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file jsonreflow-0.8.0.tar.gz.

File metadata

  • Download URL: jsonreflow-0.8.0.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for jsonreflow-0.8.0.tar.gz
Algorithm Hash digest
SHA256 09d6db8f99e367efdb51dda51161092ba91cd3118e240c1e80d74d3bbb04c143
MD5 13bded709e0a804cd425be7a939f926b
BLAKE2b-256 5f7511389f3b77e7f32e04d1ddeea82a3b35b02dbaf136347d81f7d217854606

See more details on using hashes here.

File details

Details for the file jsonreflow-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: jsonreflow-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for jsonreflow-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d8a2a74e3385732db980bf0458d951606d954e0a54647521a12889f25d8f4225
MD5 5709e149946e9b1439a95d3301e7ed64
BLAKE2b-256 b6adc6eb87e52b9e55a93b0f3595b386a3ecba834270fd7282759c7a129a9a37

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.8.0 This release

2 files

0.7.1

2 files

0.7.0

2 files

0.6.0

2 files

0.5.0

2 files

0.4.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page