Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #6 from andreyvit/andreyvit

A bunch of changes to support processing my data
  • Loading branch information...
commit 95b50dc02f57b1895440ed336a44b7303579b1fb 2 parents 3992313 + d91cf05
Zachary Poley authored

Showing 3 changed files with 29 additions and 16 deletions. Show diff stats Hide diff stats

  1. +6 1 README.md
  2. +22 14 lib/jsonCommand.js
  3. +1 1  package.json
7 README.md
Source Rendered
@@ -13,7 +13,7 @@ transforming the parsed json objects.
13 13
14 14 json-command has been renamed to json in npm. all updates will be made to the json npm package.
15 15
16   -you need node v0.2.0 or higher and npm to run this program.
  16 +you need node v0.4.0 or higher and npm to run this program.
17 17
18 18 ## Installation
19 19
@@ -62,6 +62,8 @@ please note that the last two examples require you to enter your username and pa
62 62
63 63 new.key=old_key move old_key to new.key in output object
64 64
  65 + -a input object is an array, process each element separately
  66 +
65 67 -c "js conditional" js conditional to be run in the context of each object that determines whether an object is printed
66 68
67 69 -C print the output fields as tab delimited columns in the order specified by fields
@@ -79,6 +81,8 @@ any number of fields can be specified to be printed from each json object.
79 81 by default the structure of the original json object is maintained, however options
80 82 like -e and foo=bar allow for transforming object structure.
81 83
  84 +To remove a certain key, assign it to undefined.
  85 +
82 86 ## Standard Input Formats
83 87
84 88 * line delimited json objects
@@ -86,6 +90,7 @@ like -e and foo=bar allow for transforming object structure.
86 90 e.g. { obj1 : body }{ obj2 : body }
87 91 * file separated json objects.
88 92 e.g. cat files/* | json
  93 +* when using -a, a single JSON array
89 94
90 95 ## Order of operations
91 96
36 lib/jsonCommand.js
... ... @@ -1,5 +1,5 @@
1 1 var sys = require("sys"),
2   - Script = process.binding("evals").Script;
  2 + vm = require("vm");
3 3
4 4 /*
5 5 JSON Command class
@@ -18,13 +18,13 @@ JSON.Command = function(args) {
18 18 this.headerPassthrough = false;
19 19 this.columnOutput = false;
20 20 this.useObject = null;
  21 + this.inputIsArray = false;
21 22
22 23 this.conditionals = [];
23 24 this.executables = [];
24 25
25 26 this.stdin = null;
26 27 this.buffer = "";
27   - this.scriptObject;
28 28
29 29 if (args) { this.processArgs(args); }
30 30 };
@@ -43,6 +43,7 @@ JSON.Command.prototype.printhelp = function() {
43 43 sys.puts(" -d print debugging output including exception messages\n");
44 44 sys.puts(" -o object.path specify the path to an array to be iterated on\n");
45 45 sys.puts(" new.key=old_key move old_key to new.key in output object\n");
  46 + sys.puts(" -a input object is an array, process each element separately\n");
46 47 sys.puts(" -c \"js conditional\" js conditional to be run in the context of each object");
47 48 sys.puts(" that determines whether an object is printed\n");
48 49 sys.puts(" -C print the output fields as tab delimited columns in");
@@ -140,6 +141,9 @@ JSON.Command.prototype.processArgs = function processArgs(args) {
140 141 case "-o": // use object
141 142 this.useObject = a.shift();
142 143 break;
  144 + case "-a": // array
  145 + this.inputIsArray = true;
  146 + break;
143 147 case "-i": // use sys.inspect
144 148 this.inspectOutput = true;
145 149 break;
@@ -186,8 +190,7 @@ JSON.Command.prototype.checkConditionals = function(parsedObject) {
186 190 try {
187 191 var conditionsFailed = false;
188 192 for(var i = 0; (i < this.conditionals.length); i++) {
189   - this.scriptObject = new Script("(" + this.conditionals[i] + ")");
190   - if (!this.scriptObject.runInNewContext(parsedObject)) {
  193 + if (!vm.runInNewContext(this.conditionals[i], parsedObject)) {
191 194 conditionsFailed = true;
192 195 }
193 196 }
@@ -213,9 +216,8 @@ JSON.Command.prototype.processKeyTransforms = function(parsedObject) {
213 216 if (this.transformedKeys.length) {
214 217 for(var i = 0; (i < this.transformedKeys.length); i++) {
215 218 try {
216   - this.scriptObject = new Script(this.transformedKeys[i].newKey +
217   - " = " + this.transformedKeys[i].oldKey );
218   - this.scriptObject.runInNewContext(parsedObject);
  219 + vm.runInNewContext(this.transformedKeys[i].newKey +
  220 + " = " + this.transformedKeys[i].oldKey, parsedObject)
219 221 }
220 222 catch (ex) {
221 223 this.printex(ex);
@@ -232,10 +234,7 @@ JSON.Command.prototype.processExecutables = function(parsedObject) {
232 234 if (this.executables.length) {
233 235 for(var i = 0; (i < this.executables.length); i++) {
234 236 try {
235   - // create a new script object for the executable
236   - this.scriptObject = new Script(this.executables[i]);
237   - // run the new script in the context of the parsed object
238   - this.scriptObject.runInNewContext(parsedObject);
  237 + vm.runInNewContext(this.executables[i], parsedObject);
239 238 }
240 239 catch (ex) {
241 240 // stop catstrophic failure if any executable fails.
@@ -330,15 +329,21 @@ JSON.Command.prototype.processObjects = function(objects) {
330 329 var rawObject = null, parsedObject = null;
331 330
332 331 try {
333   - if (this.useObject && objects && objects.length > 0) {
334   - this.scriptObject = new Script(this.useObject);
335   - objects = this.scriptObject.runInNewContext(JSON.parse(objects[0]));
  332 + if (this.useObject && objects && objects.length > 0 && typeof(objects[0]) == 'string' && objects[0].trim().length > 0) {
  333 + objects = vm.runInNewContext(this.useObject, JSON.parse(objects[0]));
336 334 }
337 335 }
338 336 catch(ex) {
339 337 this.printex(ex);
340 338 }
341 339
  340 + if (this.inputIsArray && objects && objects.length > 0 && typeof(objects[0]) == 'string') {
  341 + var trimmed = objects[0].trim();
  342 + if (trimmed.length > 0 && trimmed[0] == '[') {
  343 + objects = JSON.parse(objects[0]);
  344 + }
  345 + }
  346 +
342 347 try {
343 348 for (var i = 0; (i < (objects.length)); i++) {
344 349 // if there's no object, there's nothing to do
@@ -348,6 +353,7 @@ JSON.Command.prototype.processObjects = function(objects) {
348 353 try {
349 354 if (typeof(objects[i]) == "string") {
350 355 rawObject = objects[i];
  356 + if (rawObject.trim().length == 0) continue;
351 357 parsedObject = JSON.parse(rawObject);
352 358 }
353 359 else {
@@ -409,6 +415,8 @@ JSON.Command.prototype.processInput = function() {
409 415
410 416 this.stdin.on("data", function(chunk) {
411 417 this.jsonC.buffer += chunk;
  418 + if (this.jsonC.inputIsArray) return;
  419 +
412 420 var objects = null;
413 421 if (this.jsonC.buffer.match(/\n/g) ||
414 422 this.jsonC.buffer.match(/\r\n/g) ||
2  package.json
... ... @@ -1,6 +1,6 @@
1 1 { "name": "json"
2 2 , "version": "0.0.8"
3   -, "engines": [ "node >=0.2.0" ]
  3 +, "engines": [ "node >=0.4.0" ]
4 4 , "description": "JSON command line processing toolkit."
5 5 , "author": "Zachary Poley <zpoley@gmail.com> (http://zpoley.net)"
6 6 , "contributors": [ "Isaac Schlueter <i@izs.me> (http://blog.izs.me)" ]

0 comments on commit 95b50dc

Please sign in to comment.
Something went wrong with that request. Please try again.