skip to Main Content

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

2

Answers


  1. 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"]]
    
    Login or Signup to reply.
  2. 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}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search