Permalink
Browse files

Merge pull request #6 from andreyvit/andreyvit

A bunch of changes to support processing my data
  • Loading branch information...
2 parents 3992313 + d91cf05 commit 95b50dc02f57b1895440ed336a44b7303579b1fb @zpoley committed Dec 28, 2011
Showing with 29 additions and 16 deletions.
  1. +6 −1 README.md
  2. +22 −14 lib/jsonCommand.js
  3. +1 −1 package.json
View
7 README.md
@@ -13,7 +13,7 @@ transforming the parsed json objects.
json-command has been renamed to json in npm. all updates will be made to the json npm package.
-you need node v0.2.0 or higher and npm to run this program.
+you need node v0.4.0 or higher and npm to run this program.
## Installation
@@ -62,6 +62,8 @@ please note that the last two examples require you to enter your username and pa
new.key=old_key move old_key to new.key in output object
+ -a input object is an array, process each element separately
+
-c "js conditional" js conditional to be run in the context of each object that determines whether an object is printed
-C print the output fields as tab delimited columns in the order specified by fields
@@ -79,13 +81,16 @@ any number of fields can be specified to be printed from each json object.
by default the structure of the original json object is maintained, however options
like -e and foo=bar allow for transforming object structure.
+To remove a certain key, assign it to undefined.
+
## Standard Input Formats
* line delimited json objects
* back to back json objects.
e.g. { obj1 : body }{ obj2 : body }
* file separated json objects.
e.g. cat files/* | json
+* when using -a, a single JSON array
## Order of operations
View
36 lib/jsonCommand.js
@@ -1,5 +1,5 @@
var sys = require("sys"),
- Script = process.binding("evals").Script;
+ vm = require("vm");
/*
JSON Command class
@@ -18,13 +18,13 @@ JSON.Command = function(args) {
this.headerPassthrough = false;
this.columnOutput = false;
this.useObject = null;
+ this.inputIsArray = false;
this.conditionals = [];
this.executables = [];
this.stdin = null;
this.buffer = "";
- this.scriptObject;
if (args) { this.processArgs(args); }
};
@@ -43,6 +43,7 @@ JSON.Command.prototype.printhelp = function() {
sys.puts(" -d print debugging output including exception messages\n");
sys.puts(" -o object.path specify the path to an array to be iterated on\n");
sys.puts(" new.key=old_key move old_key to new.key in output object\n");
+ sys.puts(" -a input object is an array, process each element separately\n");
sys.puts(" -c \"js conditional\" js conditional to be run in the context of each object");
sys.puts(" that determines whether an object is printed\n");
sys.puts(" -C print the output fields as tab delimited columns in");
@@ -140,6 +141,9 @@ JSON.Command.prototype.processArgs = function processArgs(args) {
case "-o": // use object
this.useObject = a.shift();
break;
+ case "-a": // array
+ this.inputIsArray = true;
+ break;
case "-i": // use sys.inspect
this.inspectOutput = true;
break;
@@ -186,8 +190,7 @@ JSON.Command.prototype.checkConditionals = function(parsedObject) {
try {
var conditionsFailed = false;
for(var i = 0; (i < this.conditionals.length); i++) {
- this.scriptObject = new Script("(" + this.conditionals[i] + ")");
- if (!this.scriptObject.runInNewContext(parsedObject)) {
+ if (!vm.runInNewContext(this.conditionals[i], parsedObject)) {
conditionsFailed = true;
}
}
@@ -213,9 +216,8 @@ JSON.Command.prototype.processKeyTransforms = function(parsedObject) {
if (this.transformedKeys.length) {
for(var i = 0; (i < this.transformedKeys.length); i++) {
try {
- this.scriptObject = new Script(this.transformedKeys[i].newKey +
- " = " + this.transformedKeys[i].oldKey );
- this.scriptObject.runInNewContext(parsedObject);
+ vm.runInNewContext(this.transformedKeys[i].newKey +
+ " = " + this.transformedKeys[i].oldKey, parsedObject)
}
catch (ex) {
this.printex(ex);
@@ -232,10 +234,7 @@ JSON.Command.prototype.processExecutables = function(parsedObject) {
if (this.executables.length) {
for(var i = 0; (i < this.executables.length); i++) {
try {
- // create a new script object for the executable
- this.scriptObject = new Script(this.executables[i]);
- // run the new script in the context of the parsed object
- this.scriptObject.runInNewContext(parsedObject);
+ vm.runInNewContext(this.executables[i], parsedObject);
}
catch (ex) {
// stop catstrophic failure if any executable fails.
@@ -330,15 +329,21 @@ JSON.Command.prototype.processObjects = function(objects) {
var rawObject = null, parsedObject = null;
try {
- if (this.useObject && objects && objects.length > 0) {
- this.scriptObject = new Script(this.useObject);
- objects = this.scriptObject.runInNewContext(JSON.parse(objects[0]));
+ if (this.useObject && objects && objects.length > 0 && typeof(objects[0]) == 'string' && objects[0].trim().length > 0) {
+ objects = vm.runInNewContext(this.useObject, JSON.parse(objects[0]));
}
}
catch(ex) {
this.printex(ex);
}
+ if (this.inputIsArray && objects && objects.length > 0 && typeof(objects[0]) == 'string') {
+ var trimmed = objects[0].trim();
+ if (trimmed.length > 0 && trimmed[0] == '[') {
+ objects = JSON.parse(objects[0]);
+ }
+ }
+
try {
for (var i = 0; (i < (objects.length)); i++) {
// if there's no object, there's nothing to do
@@ -348,6 +353,7 @@ JSON.Command.prototype.processObjects = function(objects) {
try {
if (typeof(objects[i]) == "string") {
rawObject = objects[i];
+ if (rawObject.trim().length == 0) continue;
parsedObject = JSON.parse(rawObject);
}
else {
@@ -409,6 +415,8 @@ JSON.Command.prototype.processInput = function() {
this.stdin.on("data", function(chunk) {
this.jsonC.buffer += chunk;
+ if (this.jsonC.inputIsArray) return;
+
var objects = null;
if (this.jsonC.buffer.match(/\n/g) ||
this.jsonC.buffer.match(/\r\n/g) ||
View
2 package.json
@@ -1,6 +1,6 @@
{ "name": "json"
, "version": "0.0.8"
-, "engines": [ "node >=0.2.0" ]
+, "engines": [ "node >=0.4.0" ]
, "description": "JSON command line processing toolkit."
, "author": "Zachary Poley <zpoley@gmail.com> (http://zpoley.net)"
, "contributors": [ "Isaac Schlueter <i@izs.me> (http://blog.izs.me)" ]

0 comments on commit 95b50dc

Please sign in to comment.