Home > database >  jq streaming eats first element, why?
jq streaming eats first element, why?

Time:10-05

When using jq in streaming mode (because 100 GB file) it eats the first element. How can I avoid that?

echo [{"id":482,"a":"2","b":1},{"id":483,"a":"3","b":2}] | jq  -c --stream "fromstream(1|truncate_stream(inputs))"

Output is

{"a":"2","b":1}
{"id":483,"a":"3","b":2}

The first element (id) is missing from the first array element.

This is jq version 1.6. It is on Windows 2010, but the same behaviour is also on jq 1.6 on Ubuntu 22.04.

Thanks

CodePudding user response:

Use the --null-input (or -n) flag. That way the standard input is set to null, and inputs can fetch all actual inputs.

jq -cn --stream "fromstream(1|truncate_stream(inputs))"
{"id":482,"a":"2","b":1}
{"id":483,"a":"3","b":2}

CodePudding user response:

It seems that's because you're using inputs with --stream.

$ echo '[{"id":482,"a":"2","b":1},{"id":483,"a":"3","b":2}]' \
| jq  -c --stream 'inputs'
[[0,"a"],"2"]
[[0,"b"],1]
[[0,"b"]]
[[1,"id"],483]
[[1,"a"],"3"]
[[1,"b"],2]
[[1,"b"]]
[[1]]

Using . seems better, but I'm not sure what you're after:

$ echo '[{"id":482,"a":"2","b":1},{"id":483,"a":"3","b":2}]' \
| jq  -c --stream '. as $i | (1 | truncate_stream($i))'
[["id"],482]
[["a"],"2"]
[["b"],1]
[["b"]]
[["id"],483]
[["a"],"3"]
[["b"],2]
[["b"]]
  • Related