json将一行中的每个对象格式化 [英] json format each object in a line

查看:243
本文介绍了json将一行中的每个对象格式化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cat 2.txt | ./jq '{(.id): .custom}'

命令输出上方

{
  "1": {
    "results": "Success"
  }
}
{
  "2": {

    "input method": "touch",
    "from": "Prescription Center",

  }
}
{
  "3": {

    "entry point": "|All"
  }

}

预期输出:

我要打印/保存一行中的每个对象.

I want to print/save each object in a line.

cat 2.txt | ./jq '{(.id): .custom}'

{ "1": {  "results": "Success" }  }
{ "2": {  "input method": "touch",  "from": "Prescription Center"  }  }
{ "3": {  "entry point": "|All" } }

可以在shell脚本中使用吗?

will it be possible in shell script?

推荐答案

根据 jq手册

  • --compact-output/-c:

  • --compact-output / -c:

默认情况下,jq漂亮打印JSON输出.通过将每个JSON对象放在一行中,使用此选项将产生更紧凑的输出.

By default, jq pretty-prints JSON output. Using this option will result in more compact output by instead putting each JSON object on a single line.

因此,以下内容应该起作用:

Therefore the following should work:

cat 2.txt | ./jq -c '{(.id): .custom}'

这篇关于json将一行中的每个对象格式化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆