如何将文本行转换为JSON行?

如何将文本行转换为JSON行?,json,text,terminal,jsonlines,Json,Text,Terminal,Jsonlines,如果您有一个包含多行文本的文本文件,有没有现成的方法将其转换为JSON行格式 示例文本文件包含: This is the first line. This is the "second" line. This is the \third/ line. This is the {fourth} line; JSON行(.jsonl)文件示例: 我希望有一种简单的方法可以像这样对其进行线性变换,同时对JSON的特殊字符进行转义。Mac是否有在线或(CLI)工具可以做到这一点?如

如果您有一个包含多行文本的文本文件,有没有现成的方法将其转换为JSON行格式

示例文本文件包含:

This is the first line.
This is the "second" line.
This is the \third/ line.
This is the {fourth} line;
JSON行(.jsonl)文件示例:


我希望有一种简单的方法可以像这样对其进行线性变换,同时对JSON的特殊字符进行转义。Mac是否有在线或(CLI)工具可以做到这一点?

如今,浏览器是访问解释器最方便的方式。以下是一个基于此的快速解决方案:

将行复制并粘贴到此页面:

然后复制转义字符串,在modern broser上按F12并粘贴以下代码:

console.log(JSON.stringify(***粘贴在这里**).split(“\n”).map(x=>({text:x})))

然后删除
***粘贴到此处***
并粘贴转义行。按enter键,您将获得与所需内容类似的JSON输出。

谢谢,很快就到了!输出如下:[{“text”:“鱼”吃汤;”},{“text”:“汤没有鱼。”}]@codingChicken这是一个有效的JSON数组,我认为它会更有用。尝试
console.log(“***粘贴在这里**”.split(“\n”).map(x=>
{text:${JSON.stringify(x)}}
).join(“\n”)
{"text": "This is the first line."}
{"text": "This is the \"second\" line."}
{"text": "This is the \\third\/ line."}
{"text": "This is the {fourth} line;"}