如何使用Benthos读取美化JSON并转换为紧凑格式?
如何用Benthos读取多行美化JSON并压缩输出
给定美化格式的JSON文件:
{ "years": [ "2018", "2019" ] }
使用Benthos读取时,默认lines/multipart codec会将每行当作单独消息,需要调整配置让输出与jq -c ./init.json的单行压缩结果一致。以下是两种可行方案:
方案1:修改输入Codec直接读取完整文件
将输入的codec改为whole,让Benthos把整个文件作为单条消息读取,再通过json处理器压缩:
input: file: paths: [./init.json] codec: whole pipeline: processors: - json: compact: true output: type: stdout
方案2:保留lines/multipart codec,通过处理器合并压缩
如果必须保留lines/multipart codec,可先用join处理器合并所有行,再压缩JSON:
input: file: paths: [./init.json] codec: lines/multipart pipeline: processors: - join: parts: 0 separator: "" - json: compact: true output: type: stdout
说明
join处理器中parts: 0表示合并所有输入消息(即文件的每一行),separator: ""确保合并时不添加多余字符,还原完整JSON结构。json处理器的compact: true参数会将美化格式的JSON压缩为单行,与jq -c的输出效果完全一致。
内容的提问来源于stack exchange,提问作者nerdizzle
相关产品推荐
相关产品推荐

