json format each object in a line

China☆狼群 提交于 2020-05-08 04:39:44

问题


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

above command outputs

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

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

  }
}
{
  "3": {

    "entry point": "|All"
  }

}

Expected output :

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" } }

will it be possible in shell script?


回答1:


Per the jq manual

  • --compact-output / -c:

    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}'


来源:https://stackoverflow.com/questions/25467344/json-format-each-object-in-a-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!