Read JSON from STDIN and return it as a parsed JavaScript object — perfect for async/await workflows, CLI tools, and scripts that consume JSON input. Zero dependencies, MIT license.
const jsonIn = require('json-in');
(async () => {
const data = await jsonIn();
console.log('Got JSON:', data);
})();npm install json-injsonIn() reads everything from standard input (process.stdin by default), parses it as UTF-8 JSON, and resolves to the resulting object.
If nothing is piped into STDIN (for example, if run interactively in a terminal), it resolves to null.
// demo.js
const jsonIn = require('json-in');
(async () => {
const obj = await jsonIn();
if (!obj) {
console.error('No input provided.');
process.exit(1);
}
console.log('Received JSON object:', obj);
})();Then:
echo '{"hello": "world"}' | node demo.js
# => Received JSON object: { hello: 'world' }
node demo.js
# => No input provided.| Parameter | Type | Default | Description |
|---|---|---|---|
input |
Readable |
process.stdin |
Optional input stream. |
| Returns | Promise |
- | Resolves with parsed JSON or null if no data or no pipe. |
- If
stdinis a TTY, returnsnull. - Buffers all data until end-of-stream, then calls
JSON.parse(). - Handles UTF-8 input by default.
- Rejects if invalid JSON or a stream error occurs.
You can use json-in in a shell pipeline:
echo '{"foo": 42}' | node -e 'require("json-in")().then(console.log)'Or in your own command-line tools:
#!/usr/bin/env node
const jsonIn = require('json-in');
(async () => {
const input = await jsonIn();
if (!input) return console.error('Expected JSON on stdin.');
console.log('Sum:', input.a + input.b);
})();Because sometimes you just need to read JSON from STDIN — no fuss, no while-loops, no boilerplate. I could not find an NPM module that did this.
This module is:
- Tiny (<30 lines, no deps)
- Promise-based (async/await friendly)
- Safe (returns
nullwhen nothing piped) - Portable (works on Node ≥10)
AI was used to help write this module, and this README.
MIT