Skip to content

pixlcore/json-in

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

json-in

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);
})();

Installation

npm install json-in

Usage

jsonIn() 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.

Example

// 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.

API

jsonIn(input?: stream.Readable) → Promise<object|null>

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.

Behavior

  • If stdin is a TTY, returns null.
  • 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.

Example CLI use case

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);
})();

Why?

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 null when nothing piped)
  • Portable (works on Node ≥10)

AI Disclaimer

AI was used to help write this module, and this README.

License

MIT

About

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.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors