Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[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
@trentm authored
View
4 CHANGES.md
@@ -2,7 +2,9 @@
## json 2.0.1 (not yet released)
-(nothing yet)
+- [issue#23] Fix output in '-a|--array' mode if one or more keys don't
+ exist in one or more of the array items.
+
## json 2.0.0
View
10 lib/jsontool.js
@@ -399,7 +399,7 @@ function lookupDatum(datum, lookup) {
/**
* Print out a single result, considering input options.
*/
-function printDatum(datum, opts, sep) {
+function printDatum(datum, opts, sep, alwaysPrintSep) {
var output = null;
switch (opts.outputMode) {
case OM_INSPECT:
@@ -440,6 +440,8 @@ function printDatum(datum, opts, sep) {
if (output && output.length) {
emit(output);
emit(sep);
+ } else if (alwaysPrintSep) {
+ emit(sep);
}
}
@@ -560,9 +562,9 @@ function main(argv) {
results.forEach(function (row) {
var c;
for (c = 0; c < row.length-1; c++) {
- printDatum(row[c], opts, opts.delim);
+ printDatum(row[c], opts, opts.delim, true);
}
- printDatum(row[c], opts, '\n');
+ printDatum(row[c], opts, '\n', true);
});
} else {
if (lookups.length === 0) {
@@ -573,7 +575,7 @@ function main(argv) {
}
}
results.forEach(function (r) {
- printDatum(r, opts, '\n');
+ printDatum(r, opts, '\n', false);
});
}
});
View
6 test/array-missing-keys/cmd
@@ -0,0 +1,6 @@
+JSON=../../lib/jsontool.js
+cat input | $JSON -a a
+echo ""
+cat input | $JSON -a a b
+echo ""
+cat input | $JSON -a a b -d,
View
8 test/array-missing-keys/expected.stdout
@@ -0,0 +1,8 @@
+1
+2
+
+1
+2 3
+
+1,
+2,3
View
1  test/array-missing-keys/input
@@ -0,0 +1 @@
+[{"a":1}, {"a":2, "b":3}]
Please sign in to comment.
Something went wrong with that request. Please try again.