Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json format each object in a line

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?

like image 580
user2711819 Avatar asked Dec 09 '22 06:12

user2711819


1 Answers

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}'
like image 73
Miller Avatar answered Dec 24 '22 10:12

Miller