vscode中jsonl转json
时间: 2025-04-20 17:30:09 浏览: 42
### 如何在 VSCode 中将 JSONL 文件转换为标准 JSON 格式
为了实现这一目标,可以编写一段简单的 Python 脚本来读取 JSON Lines (JSONL) 文件并将其转换成标准的 JSON 数组格式。此过程涉及逐行解析每一行作为独立的 JSON 对象,并最终将这些对象收集到一个列表中再导出。
#### 使用 Python 进行转换
下面展示了一个完整的 Python 解决方案来完成这个任务:
```python
import json
def jsonl_to_json(jsonl_file_path, output_file_path):
items = []
with open(jsonl_file_path, 'r', encoding='utf-8') as file:
for line in file:
item = json.loads(line.strip())
items.append(item)
with open(output_file_path, 'w', encoding='utf-8') as out_file:
json.dump(items, out_file, ensure_ascii=False, indent=4)
if __name__ == "__main__":
input_filepath = "input.jsonl"
output_filepath = "output.json"
jsonl_to_json(input_filepath, output_filepath)
```
这段脚本会打开指定路径下的 `.jsonl` 文件,按照行读入数据并将每条记录作为一个单独的对象加入到 `items` 列表里;之后它将以美观打印的方式写出整个列表至新的 `.json` 文件[^1]。
对于那些更倾向于直接利用 VSCode 功能而不愿额外运行外部脚本的人来说,在扩展市场中寻找专门用于处理 JSON 和 JSON Lines 的插件可能是更好的选择。安装合适的插件后,通常可以通过右键菜单选项轻松地执行此类操作[^2]。
阅读全文
相关推荐

















