You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 14:02:44