btiernay
btiernay

Reputation: 8129

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.

Upvotes: 2

Views: 1855

Answers (1)

Jeff Mercado
Jeff Mercado

Reputation: 134521

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.

Upvotes: 4

Related Questions