2

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.

peak
  • 105,803
  • 17
  • 152
  • 177
btiernay
  • 7,873
  • 5
  • 42
  • 48
  • The JSONL specification requires: "Line Separator is '\n'"; thus if the file is JSONL as posited, using standard *ix tools such as `head` would be fine, unless of course you want the output to be pretty-printed. Be warned, though, that jq does not guarantee that numerical values are preserved. – peak Jul 18 '17 at 15:44

1 Answers1

4

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.

btiernay
  • 7,873
  • 5
  • 42
  • 48
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • Note that, if you want to _emit_ JSONL, jq has the -c flag which does that nicely. –  Mar 02 '16 at 21:36