Search code examples
jsonjqjsonlines

Efficiently get the first record of a JSONL file


Is it possible to efficiently get the first record of a JSONL file without consuming the entire stream / file? One way I have been able to inefficiently do so is the following:

curl -s http://example.org/file.jsonl | jq -s '.[0]'

I realize that head could be used here to extract the first line, but assume that the file may not use a newline as the record separator and may simply be concatenated objects or arrays.


Solution

  • If I'm understanding correctly, the JSONL format just returns a stream of JSON objects which jq handles quite nicely. Best case scenario that you wanted the first item, you could just utilize the input filter to grab the first item.

    I think you could just do this:

    $ curl -s http://example.org/file.jsonl | jq -n 'input'
    

    You need the null input -n to not process the input immediately then input just gets one input from the stream. No need to go through the rest of the input stream.