Skip to content

Commit

Permalink
[issue#23] Fix output in '-a|--array' mode if one or more keys don't …
Browse files Browse the repository at this point in the history
…exist in one or more of the array items.
  • Loading branch information
trentm committed Oct 25, 2011
1 parent dffbf67 commit f43c627
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
4 changes: 3 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 6 additions & 4 deletions lib/jsontool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -440,6 +440,8 @@ function printDatum(datum, opts, sep) {
if (output && output.length) {
emit(output);
emit(sep);
} else if (alwaysPrintSep) {
emit(sep);
}
}

Expand Down Expand Up @@ -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) {
Expand All @@ -573,7 +575,7 @@ function main(argv) {
}
}
results.forEach(function (r) {
printDatum(r, opts, '\n');
printDatum(r, opts, '\n', false);
});
}
});
Expand Down
6 changes: 6 additions & 0 deletions test/array-missing-keys/cmd
Original file line number Diff line number Diff line change
@@ -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,
8 changes: 8 additions & 0 deletions test/array-missing-keys/expected.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1
2

1
2 3

1,
2,3
1 change: 1 addition & 0 deletions test/array-missing-keys/input
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"a":1}, {"a":2, "b":3}]

0 comments on commit f43c627

Please sign in to comment.