Is there a way to convert multiline, non-JSON input to a JSON array of lines in one invocation of jq?
Desired result:
echo -en "foo\nbar\nbaz" | jq <unknown set of options and expression>
[
"foo",
"bar",
"baz"
]
Failed attempts:
$ echo -en "foo\nbar\nbaz" | jq --raw-input '[.]'
[
"foo"
]
[
"bar"
]
[
"baz"
]
$ echo -en "foo\nbar\nbaz" | jq --raw-input --slurp '[.]'
[
"foo\nbar\nbaz"
]
I did manage to get the result that I want, but only by using --raw-input and --slurp separately:
$ echo -en "foo\nbar\nbaz" | jq --raw-input . | jq --slurp .
[
"foo",
"bar",
"baz"
]
Is that the preferred way?
Use split/1:
$ jq --raw-input --slurp 'split("\n")' query.txt
[
"foo",
"bar",
"baz",
""
]
But as shown above you might have to be careful about trailing carriage returns.
Thanks for the tip.
| .[0:-1] will remove that last empty element.
jq --raw-input --slurp 'split("\n") | map(select(. != ""))' query.txt
removes all the blank lines, assuming that's what you want to do.
If your jq has inputs then to avoid having to read the whole file into memory up front, it would be better to use it, e.g. if empty lines are to be omitted:
jq -nR '[inputs | select(length>0)]'
How about using the --args flag:
$ jq -n '$ARGS.positional' --args foo bar baz
[
"foo",
"bar",
"baz"
]
Most helpful comment
If your jq has
inputsthen to avoid having to read the whole file into memory up front, it would be better to use it, e.g. if empty lines are to be omitted: