将 JSON 行序列 (JSONL) 转换为 JSON 数组

Convert sequence of JSON lines (JSONL) to JSON array

我有一个文件,其中每一行都是一个 JSON 对象。我想将文件转换为 JSON 数组。

文件看起来像这样:

{"address":"email1@foo.bar.com", "topic":"Some topic."}
{"address":"email2@foo.bar.com", "topic":"Another topic."}
{"address":"email3@foo.bar.com", "topic":"Yet another topic."}

我正在使用 bash 和 jq。

我试过了

jq --slurp --raw-input 'split("\n")[:-1]' my_file

但这只是将每一行视为一个字符串,创建一个 JSON 字符串数组。

[
  "{\"address\":\"email1@foo.bar.com\", \"topic\":\"Some topic.\"}",
  "{\"address\":\"email2@foo.bar.com\", \"topic\":\"Another topic.\"}",
  "{\"address\":\"email3@foo.bar.com\", \"topic\":\"Yet another topic.\"}"
]

我想要:

[
  {"address":"email1@foo.bar.com", "topic":"Some topic."},
  {"address":"email2@foo.bar.com", "topic":"Another topic."},
  {"address":"email3@foo.bar.com", "topic":"Yet another topic."}
]
jq -n '[inputs]' <in.jsonl >out.json

...或者,如 :

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

对于手头的任务,使用 jq 的 "slurp" 选项或 [inputs] 可能会造成巨大的资源浪费。

一个简单但有效的解决方案可以在 awk 中实现如下:

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

使用 foreachinputs 可以在 jq 中得到等效的有效解决方案,留作练习。