Skip to content

Instantly share code, notes, and snippets.

@vicapow
Created May 31, 2015 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vicapow/a143c8dc2de2654ead1f to your computer and use it in GitHub Desktop.
Save vicapow/a143c8dc2de2654ead1f to your computer and use it in GitHub Desktop.
js-to-json
#! /usr/local/bin/node
var vm = require('vm');
var Writable = require('stream').Writable;
var ws = Writable();
var chunks = [];
ws._write = function (chunk, enc, next) {
chunks.push(chunk);
next();
};
process.stdin.on('end', function() {
var obj = Buffer.concat(chunks).toString();
var sandbox = {obj: null};
vm.runInNewContext('obj = ' + obj, sandbox);
console.log(JSON.stringify(sandbox.obj, null, 2));
});
process.stdin.pipe(ws);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment