Skip to content
This repository
Browse code

[issue#23] Fix output in '-a|--array' mode if one or more keys don't …

…exist in one or more of the array items.
  • Loading branch information...
commit f43c6274065529ef2222e1c6bfddfd16f0e6049c 1 parent dffbf67
Trent Mick authored
4  CHANGES.md
Source Rendered
@@ -2,7 +2,9 @@
2 2
 
3 3
 ## json 2.0.1 (not yet released)
4 4
 
5  
-(nothing yet)
  5
+- [issue#23] Fix output in '-a|--array' mode if one or more keys don't
  6
+  exist in one or more of the array items.
  7
+
6 8
 
7 9
 ## json 2.0.0
8 10
 
10  lib/jsontool.js
@@ -399,7 +399,7 @@ function lookupDatum(datum, lookup) {
399 399
 /**
400 400
  * Print out a single result, considering input options.
401 401
  */
402  
-function printDatum(datum, opts, sep) {
  402
+function printDatum(datum, opts, sep, alwaysPrintSep) {
403 403
   var output = null;
404 404
   switch (opts.outputMode) {
405 405
   case OM_INSPECT:
@@ -440,6 +440,8 @@ function printDatum(datum, opts, sep) {
440 440
   if (output && output.length) {
441 441
     emit(output);
442 442
     emit(sep);
  443
+  } else if (alwaysPrintSep) {
  444
+    emit(sep);
443 445
   }
444 446
 }
445 447
 
@@ -560,9 +562,9 @@ function main(argv) {
560 562
       results.forEach(function (row) {
561 563
         var c;
562 564
         for (c = 0; c < row.length-1; c++) {
563  
-          printDatum(row[c], opts, opts.delim);
  565
+          printDatum(row[c], opts, opts.delim, true);
564 566
         }
565  
-        printDatum(row[c], opts, '\n');
  567
+        printDatum(row[c], opts, '\n', true);
566 568
       });
567 569
     } else {
568 570
       if (lookups.length === 0) {
@@ -573,7 +575,7 @@ function main(argv) {
573 575
         }
574 576
       }
575 577
       results.forEach(function (r) {
576  
-        printDatum(r, opts, '\n');
  578
+        printDatum(r, opts, '\n', false);
577 579
       });
578 580
     }
579 581
   });
6  test/array-missing-keys/cmd
... ...
@@ -0,0 +1,6 @@
  1
+JSON=../../lib/jsontool.js
  2
+cat input | $JSON -a a
  3
+echo ""
  4
+cat input | $JSON -a a b
  5
+echo ""
  6
+cat input | $JSON -a a b -d,
8  test/array-missing-keys/expected.stdout
... ...
@@ -0,0 +1,8 @@
  1
+1
  2
+2
  3
+
  4
+1 
  5
+2 3
  6
+
  7
+1,
  8
+2,3
1  test/array-missing-keys/input
... ...
@@ -0,0 +1 @@
  1
+[{"a":1}, {"a":2, "b":3}]

0 notes on commit f43c627

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