Convert sequence of JSON lines (JSONL) to JSON array Convert sequence of JSON lines (JSONL) to JSON array json json

Convert sequence of JSON lines (JSONL) to JSON array


jq -n '[inputs]' <in.jsonl >out.json

...or, as suggested by @borrible:

jq --slurp . <in.jsonl >out.json


For the task at hand, using jq's "slurp" option or [inputs] entails a potentially huge waste of resources.

A trivial but efficient solution can be implemented in awk as follows:

awk 'BEGIN {print "[";} NF==0{next;} n=="" {print;n++;next;} {print ","; print;} END {print "]"}'

An equivalent efficient solution in jq is possible using foreach and inputs, and is left as an exercise.