Skip to content

Instantly share code, notes, and snippets.

@adrienjoly
Created June 18, 2020 14:40
Show Gist options
  • Save adrienjoly/14a11cf4ce64e11ed2ce14868ad25ba9 to your computer and use it in GitHub Desktop.
Save adrienjoly/14a11cf4ce64e11ed2ce14868ad25ba9 to your computer and use it in GitHub Desktop.
A one-line node.js program to format JSON from a stdin stream, to include in bash scripts
$ echo '{"a":1}' \
| node -e \
"d=[];process.openStdin().on('data',c=>d.push(c)).on('end',()=>console.log(JSON.stringify(JSON.parse(d.join('')),null,2)));"
# =>
# {
# "a": 1
# }
@adrienjoly
Copy link
Author

const readStdin = () => new Promise((resolve) => {
  const chunks = [];
  process
    .openStdin()
    .on("data", (chunk) => chunks.push(chunk))
    .on("end", () => resolve(chunks.join("")));
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment