I have a file containing a sequence of json objects. Using the jq tool, how can I get the first json object from that file?
I have tried jq limit(1; .) input.jsonl
but that limits the length of each individual object in the file, rather than giving me just the first object. I have also read through the whole of the jq man page! Maybe I missed something.
3
Answers
Well okay one way to do it is like this:
The -c option says to output the entries one per line, then
head
gets the first line, then we pretty-print it again with jq. Surely there is a way to do this with just a single invokation of jq?For an input file like
you could
slurp the entire input and then print the first array element:
use null-input, and then
inputs
:Null input (
-n
) is useful in such a case:The
input
filter will read a single JSON element from the provided input stream.Example:
Output: