json将每个对象格式化为一行

use*_*819 3 shell json

cat 2.txt | ./jq '{(.id): .custom}'
Run Code Online (Sandbox Code Playgroud)

以上命令输出

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

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

  }
}
{
  "3": {

    "entry point": "|All"
  }

}
Run Code Online (Sandbox Code Playgroud)

预期产量:

我想在一行中打印/保存每个对象.

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

{ "1": {  "results": "Success" }  }
{ "2": {  "input method": "touch",  "from": "Prescription Center"  }  }
{ "3": {  "entry point": "|All" } }
Run Code Online (Sandbox Code Playgroud)

shell脚本可以吗?

Mil*_*ler 12

根据jq手册

  • --compact-output/ -c:

    默认情况下,jq漂亮打印JSON输出.使用此选项将导致更紧凑的输出,而不是将每个JSON对象放在一行上.

因此以下应该有效:

cat 2.txt | ./jq -c '{(.id): .custom}'
Run Code Online (Sandbox Code Playgroud)

  • `jq` 太可爱​​了。 (3认同)