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

时间:2014-08-23 23:44:53

标签: json shell

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

以上命令输出

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

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

  }
}
{
  "3": {

    "entry point": "|All"
  }

}

预期产出:

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

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

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

可以在shell脚本中使用吗?

1 个答案:

答案 0 :(得分:11)

根据jq manual

  
      
  • --compact-output / -c

         

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

  •   

因此以下内容应该有效:

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