7
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?

peak
  • 105,803
  • 17
  • 152
  • 177
user2711819
  • 960
  • 3
  • 16
  • 29
  • 1
    [Have you tried `cat 2.txt | ./jq -c '{(.id): .custom}'`](http://stedolan.github.io/jq/manual/) – Prix Aug 23 '14 at 23:57
  • 1
    -c option works , Thank you. you can post it in answer i will accept. jq is c program to parse json – user2711819 Aug 24 '14 at 00:28
  • My Ubuntu doesn't know any `jq`, and why do you have it in your local directory? I'd be interested in that tool. – Alfe Aug 24 '14 at 00:34
  • 1
    @Alfe, `sudo apt-get install jq` – glenn jackman Aug 24 '14 at 00:40
  • Funny. `apt-file` didn't show that, but `apt-get` could install it. I guess I have to learn more about those two package management tools. Thanks! – Alfe Aug 24 '14 at 00:42
  • Ah, an `apt-file update` did the trick; now the `apt-file list` also displays the `jq` package. Seems to have appeared in the last months/years (before my last `apt-file update`). – Alfe Aug 24 '14 at 21:36

1 Answers1

17

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}'
Miller
  • 34,962
  • 4
  • 39
  • 60