From f43c6274065529ef2222e1c6bfddfd16f0e6049c Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 25 Oct 2011 14:44:23 -0700 Subject: [PATCH] [issue#23] Fix output in '-a|--array' mode if one or more keys don't exist in one or more of the array items. --- CHANGES.md | 4 +++- lib/jsontool.js | 10 ++++++---- test/array-missing-keys/cmd | 6 ++++++ test/array-missing-keys/expected.stdout | 8 ++++++++ test/array-missing-keys/input | 1 + 5 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 test/array-missing-keys/cmd create mode 100644 test/array-missing-keys/expected.stdout create mode 100644 test/array-missing-keys/input diff --git a/CHANGES.md b/CHANGES.md index b89c07f..3e335e8 100644 --- a/CHANGES.md +++ b/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 diff --git a/lib/jsontool.js b/lib/jsontool.js index 01efac3..1a23f7e 100755 --- a/lib/jsontool.js +++ b/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); }); } }); diff --git a/test/array-missing-keys/cmd b/test/array-missing-keys/cmd new file mode 100644 index 0000000..f10ce05 --- /dev/null +++ b/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, diff --git a/test/array-missing-keys/expected.stdout b/test/array-missing-keys/expected.stdout new file mode 100644 index 0000000..873e78e --- /dev/null +++ b/test/array-missing-keys/expected.stdout @@ -0,0 +1,8 @@ +1 +2 + +1 +2 3 + +1, +2,3 diff --git a/test/array-missing-keys/input b/test/array-missing-keys/input new file mode 100644 index 0000000..d049179 --- /dev/null +++ b/test/array-missing-keys/input @@ -0,0 +1 @@ +[{"a":1}, {"a":2, "b":3}]