Skip to content

feat: allow writing to output file #3367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

christf
Copy link

@christf christf commented Jul 4, 2025

This allows to write the output to a file and closes #2418 . When calling jq inside a docker context this will be helpful as it means jq will not have to be called from within a shell with output redirection.

Caveats do apply:

  • I have only started looking at the jq codebase today, so the code may be doing something insane

@christf christf changed the title implements #2418 - allow writing to output file feat: allow writing to output file Jul 4, 2025
@christf
Copy link
Author

christf commented Jul 8, 2025

Test on linux with utf-8:

✘-INT ~/Programmierung/jq [ofile|✚ 1…1]
19:42 $ echo '{ "foo": "💩" } '
{ "foo": "💩" }
✔ ~/Programmierung/jq [ofile|✚ 1…1]
19:42 $ echo '{ "foo": "💩" } ' | ./jq .
{
"foo": "💩"
}
✔ ~/Programmierung/jq [ofile|✚ 1…1]
19:42 $ echo '{ "foo": "💩" } ' | ./jq . -o foo
✔ ~/Programmierung/jq [ofile|✚ 1…1]
19:43 $ cat foo
{
"foo": "💩"
}
✔ ~/Programmierung/jq [ofile|✚ 1…1]
19:43 $

@wader
Copy link
Member

wader commented Jul 8, 2025

There has been talks long time ago about adding adding -i/--inplace and jaq already supports it. Could that be an option? but one thing about how jq handles multiple inputs files, as they were concatenated, probably have be resolved in some way in that case.

$ echo -n 12 > a.json
$ echo -n 34 > b.json

$ jq . a.json b.json
1234

$ jaq . a.json b.json
12
34
$ jaq -i '.+1' a.json b.json
$ jaq . a.json b.json
13
35

@christf
Copy link
Author

christf commented Jul 8, 2025

There has been talks long time ago about adding adding -i/--inplace and jaq already supports it. Could that be an option? but one thing about how jq handles multiple inputs files, as they were concatenated, probably have be resolved in some way in that case.

$ echo -n 12 > a.json
$ echo -n 34 > b.json

$ jq . a.json b.json
1234

$ jaq . a.json b.json
12
34
$ jaq -i '.+1' a.json b.json
$ jaq . a.json b.json
13
35

that is an interesting thought. In the use case I am hoping to solve, the output file needs to be in a certain place that is designed to hold only results. This is, what the tekton task specification might look like

- image: ghcr.io/jqlang/jq:1.9.0
  name: digest-to-results
  command:
    - jq -j '.[0].value' -o /tekton/results/IMAGE-DIGEST image-digested

This allows to write the output to a file and introduces the options -o
and --output-file that take a filename as parameter. When not specifying
-o, stdout will be used for compatibility.

This will be helpful when calling jq inside a docker context as it means
jq will not have to be called from within a shell with output redirection.
@christf
Copy link
Author

christf commented Aug 4, 2025

How can this best be progressed?

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.

Output to file without STDOUT redirection
2 participants