有没有一种方法可以将json对象转换为json l文件 [英] is there a way to convert a json object to json l file
问题描述
我有一个对象数组.我需要将其转换为.jsonl格式并使用lambda函数中的node作为响应发送我一直在尝试将其更改为字符串,并添加'\ n'使其换行,但没有成功
I have an array of objects. I need to convert it in .jsonl format and send it as response using node in a lambda function i have been trying to change it as a string and add '\n' to make it a new line but it didn't work
推荐答案
用于生成jsonlines的简单代码.jsonlines实际上只是一串单行的JSON对象,它们之间用换行符进行了字符串化和连接.就是这样.
您需要处理的另一个问题是转义unicode,因此在写入文件时,必须使用UTF-8编码.
Simple code to generate jsonlines. jsonlines is really just a bunch of one-line JSON objects stringified and concatenated with newlines between them. That's it.
The other issue you will need to deal with is escaping unicode, so when you write to a file, you must use UTF-8 encoding.
repl.it演示: https://repl.it/repls/AngelicGratefulMoto
repl.it demo using jsonlines npm library: https://repl.it/repls/AngelicGratefulMoto
简单的普通JS演示:
data = [{ jsonlines: 'is awesome' },{ jsonlines: 'is awesome' },{ jsonlines: 'is awesome' },{ jsonlines: 'is awesome' },{ jsonlines: 'is awesome' }]
console.log(
data.map(x=>JSON.stringify(x)).join('\n')
)
这篇关于有没有一种方法可以将json对象转换为json l文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!