Skip to content

Instantly share code, notes, and snippets.

@dimfeld
Created March 23, 2016 15:15
Show Gist options
  • Save dimfeld/306bb2ebce779962a5a8 to your computer and use it in GitHub Desktop.
Save dimfeld/306bb2ebce779962a5a8 to your computer and use it in GitHub Desktop.
Convert JS-style objects into proper JSON
#!/usr/bin/env node
"use strict";
var fs = require("fs");
var vm = require("vm");
var sandbox = { value: null };
var context = vm.createContext(sandbox);
function convert(contents) {
vm.runInContext('value = ' + contents, context);
return JSON.stringify(sandbox.value);
}
var args = process.argv.slice(2);
if(args.length) {
args.forEach(function(name) {
var contents = fs.readFileSync(name);
console.log(convert(contents));
});
} else {
var data = [];
process.stdin.on('data', function(d) { data.push(d); });
process.stdin.on('end', function() {
var contents = data.join("");
console.log(convert(contents));
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment