Skip to content

Jq --in-place semantics#1

Open
jubr wants to merge 15 commits into
masterfrom
cursor/jq-in-place-semantics-72a6
Open

Jq --in-place semantics#1
jubr wants to merge 15 commits into
masterfrom
cursor/jq-in-place-semantics-72a6

Conversation

@jubr
Copy link
Copy Markdown
Owner

@jubr jubr commented Feb 27, 2026

Add --in-place editing mode to jq with sed -i semantics.

This PR implements the --in-place option, allowing jq to modify a single input file directly, similar to sed -i. It ensures atomic updates by writing to a temporary file and then renaming it, preserving original file permissions and handling errors gracefully.

Historical context / references

Original feature request: jqlang#105
Prior attempt: jqlang#2616
Original -i implementation in jq history by @nicowilliams (commit 01fc816, later removed in b82c231)
Follow-up unhappy-path discussion: jqlang#704

Documentation (from new manual.yml)

--in-place / -i

This adds direct-file in-place editing semantics similar to sed -i:

  • the input file is also the output target;
  • jq writes to a temporary file in the same directory, then renames into place;
  • exactly one input file argument is required.

Supported usage patterns

1) Compact JSON rewrite in place

jq -c --in-place '.count += 1' state.json

2) Normalize key order in place

jq -S --in-place '.' config.json

3) Slurp + transform + write back to same file

jq -s --in-place 'map(.enabled = true)' records.json

4) Raw-text output in place (intentional non-JSON file replacement)

jq -r --in-place '.items[]' items.json

Invalid combinations / unhappy paths (now explicitly handled)

Missing file argument

jq --in-place '.+1'
# exits non-zero (usage error)

stdin target is rejected

printf '1\n' | jq --in-place '.+1' -
# exits non-zero (usage error)

Multiple file arguments are rejected

jq --in-place '.+1' a.json b.json
# exits non-zero (usage error)

--null-input cannot be combined with --in-place

jq -n --in-place '.+1' a.json
# exits non-zero (usage error)

Failure safety guarantees

If jq fails, the original input file is preserved:

  • compile error (e.g. invalid filter) leaves original file unchanged;
  • runtime error (e.g. error("boom")) leaves original file unchanged;
  • temp-file creation failure (e.g. non-writable directory) leaves original file unchanged.

Temporary output is cleaned up on error.

cursoragent and others added 2 commits February 27, 2026 09:43
Co-authored-by: Jurgen Braam <jubr@users.noreply.github.com>
Co-authored-by: Jurgen Braam <jubr@users.noreply.github.com>
@cursor
Copy link
Copy Markdown

cursor Bot commented Feb 27, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

Co-authored-by: Jurgen Braam <jubr@users.noreply.github.com>
@jubr jubr changed the title Jq in-place semantics Jq --in-place semantics Feb 27, 2026
cursoragent and others added 5 commits February 27, 2026 10:18
Co-authored-by: Jurgen Braam <jubr@users.noreply.github.com>
Co-authored-by: Jurgen Braam <jubr@users.noreply.github.com>
Co-authored-by: Jurgen Braam <jubr@users.noreply.github.com>
Co-authored-by: Jurgen Braam <jubr@users.noreply.github.com>
Co-authored-by: Jurgen Braam <jubr@users.noreply.github.com>
@jubr jubr marked this pull request as ready for review February 27, 2026 11:45
cursoragent and others added 7 commits February 27, 2026 12:05
Co-authored-by: Jurgen Braam <jubr@users.noreply.github.com>
Co-authored-by: Jurgen Braam <jubr@users.noreply.github.com>
Co-authored-by: Jurgen Braam <jubr@users.noreply.github.com>
Co-authored-by: Jurgen Braam <jubr@users.noreply.github.com>
Co-authored-by: Jurgen Braam <jubr@users.noreply.github.com>
Co-authored-by: Jurgen Braam <jubr@users.noreply.github.com>
Co-authored-by: Jurgen Braam <jubr@users.noreply.github.com>
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.

2 participants