Skip to content

json: support BORG_JSON_COMPACT env var for single-line output#9855

Open
charmikadi wants to merge 6 commits into
borgbackup:masterfrom
charmikadi:fix/json-compact-env-var
Open

json: support BORG_JSON_COMPACT env var for single-line output#9855
charmikadi wants to merge 6 commits into
borgbackup:masterfrom
charmikadi:fix/json-compact-env-var

Conversation

@charmikadi

Copy link
Copy Markdown
Contributor

Description

When BORG_JSON_COMPACT is set, json_dump() produces single-line JSON instead of the default indented output. This makes it easier for log collectors to parse borg's JSON stdout as one event per line. Implementation uses an env var and not CLI flag.

Fixes #3605

Checklist

  • PR is against master
  • New code has tests and docs where appropriate
  • Tests pass (run tox or the relevant test subset)
  • Commit messages are clean and reference related issues

@codecov

codecov Bot commented Jul 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.13%. Comparing base (5aaa2dd) to head (c322dd5).
⚠️ Report is 9 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9855      +/-   ##
==========================================
+ Coverage   85.11%   85.13%   +0.01%     
==========================================
  Files          93       93              
  Lines       15408    15421      +13     
  Branches     2326     2329       +3     
==========================================
+ Hits        13114    13128      +14     
+ Misses       1596     1595       -1     
  Partials      698      698              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@charmikadi charmikadi marked this pull request as ready for review July 4, 2026 05:33
Comment thread src/borg/helpers/parseformat.py Outdated
"""Dump using BorgJSONEncoder."""
return json.dumps(obj, sort_keys=True, indent=4, cls=BorgJsonEncoder)
indent = 4
if os.environ.get("BORG_JSON_COMPACT"):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about calling it BORG_JSON_INDENT and supporting all options that the python parameter supports?

indent (int | str | None) – If a positive integer or string, JSON array elements and object members will be pretty-printed with that indent level. A positive integer indents that many spaces per level; a string (such as "\t") is used to indent each level. If zero, negative, or "" (the empty string), only newlines are inserted. If None (the default), no newlines are inserted.

Like it is now, it could be confusing to users, because setting that to any non-empty value has the same effect. E.g. "1" would make output compact and "0" would also make output compact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No indent option on --json and --log-json

2 participants