我有一个文件,其中每一行都是一个 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."}
]