【问题标题】:How to convert lines of text to JSON Lines?如何将文本行转换为 JSON 行?
【发布时间】:2021-03-25 10:10:58
【问题描述】:

如果您有一个包含多行文本的文本文件,是否有现成的方法可以将其转换为 JSON Lines 格式?

示例文本文件包含:

This is the first line.
This is the "second" line.
This is the \third/ line.
This is the {fourth} line;

示例 JSON 行 (.jsonl) 文件:

{"text": "This is the first line."}
{"text": "This is the \"second\" line."}
{"text": "This is the \\third\/ line."}
{"text": "This is the {fourth} line;"}

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

【问题讨论】:

    标签: json text terminal jsonlines


    【解决方案1】:

    浏览器是当今访问解释器的最便捷方式。这是一个基于此的快速解决方案:

    将您的行复制并粘贴到此页面中:https://codebeautify.org/javascript-escape-unescape

    然后复制转义字符串,在现代浏览器上按 F12 并粘贴以下代码:

    console.log(JSON.stringify("***PASTE HERE**".split("\n").map(x => ({text: x}))))
    

    然后删除***PASTE HERE*** 并粘贴转义的行。按回车键,您将获得与您想要的相似的 JSON 输出。

    【讨论】:

    • 谢谢,接近了!输出如下所示: [{"text":"The \"fish\" eats soup;"},{"text":"The soup is without fish."}]
    • @codingChicken 这是一个有效的 JSON 数组,我认为它会更有用。试试console.log("***PASTE HERE**".split("\n").map(x => {text: ${JSON.stringify(x)}}).join("\n"))
    猜你喜欢
    • 2011-08-28
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多