Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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]}'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading