I just saw an example of using echo
to put multiple things into the pipeline (for jq
).
echo '{"A": {"a": 1}}' '{"A": {"b": 2}}' '{"B": 3}' |\
jq --slurp 'reduce .[] as $item ({}; . * $item)'
If I understand it right, this seems to put three objects into the pipeline on the one end, and on the other, jq
receives them and puts them into an array and reduce it with a merge operator.
This is surprising to me, and I am not sure how it works to have multiple things in the stdin
.
More specifically, how does the system know the input is three files and where each file begins/ends?
If I just do the echo part echo '{"A":1}' '{"A": {"b": 2}}' '{"B": 3}'
, the output does not seem to have any visible delimiter between the three objects:
{"A":1} {"A": {"b": 2}} {"B": 3}
Does the single-quotes have some special effects that tells the next filter (jq
) the boundaries? Or how does jq know the boundaries?
(This is with Ubuntu 24.04, jq 1.7.1)
jq
deserializes the string and processes the resulting object internally.echo
(and by extension the shell) is performing some magic to feed more than one file / multiple JSON objects into a pipeline. Obviously that doesn't represent a correct or fully-encompassing model of how things work, at all, just the model I thought they had in their mind.jq
doesn't see 3 files, just one file with 3 JSON objects