Home > Net >  how transform stdin input to some json field in list of lines using jq
how transform stdin input to some json field in list of lines using jq

Time:01-22

I need a utility that

  • read from stdin, split as lines
  • write to stdout json object, {"cmd": "In", "lines": [lines from stdin]}

I heard jq being quite powerful, how to acheive above with jq or similar tools.

CodePudding user response:

By default, jq reads from stdin. The -R flag lets you read raw input streamed linewise. Using [inputs] in combination with the -n flag lets you collect the lines into an array.

… | jq -Rn '{cmd: "In", lines: [inputs]}'
  • Related