Skip to content

Commit

Permalink
Add 'excludeKeys' option to json2csv. (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrodrig committed Dec 17, 2020
1 parent c9df78c commit 995ff6b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ Looking for examples? Check out the Wiki: [json-2-csv Wiki](https://github.com/m
* `emptyFieldValue` - Any - Value that, if specified, will be substituted in for field values that are `undefined`, `null`, or an empty string.
* Default: none
* `excelBOM` - Boolean - Should a unicode character be prepended to allow Excel to open a UTF-8 encoded file with non-ASCII characters present.
* `excludeKeys` - Array - Specify the keys that should be excluded from the output.
* Default: `[]`
* Note: When used with `unwindArrays`, arrays present at excluded key paths will not be unwound.
* `expandArrayObjects` - Boolean - Should objects in array values be deep-converted to CSV?
* Default: `false`
* Example:
Expand All @@ -82,7 +85,7 @@ Looking for examples? Check out the Wiki: [json-2-csv Wiki](https://github.com/m
* `false` uses the following keys:
* `['specifications']`
* Note: This may result in CSV output that does not map back exactly to the original JSON. See #102 for more information.
* `keys` - Array - Specify the keys (as strings) that should be converted.
* `keys` - Array - Specify the keys that should be converted.
* Default: These will be auto-detected from your data by default.
* Keys can either be specified as a String representing the key path that should be converted, or as an Object with the `key` property specifying the path. When specifying keys as an Object, you can also optionally specify a `title` which will be used for that column in the header. The list specified can contain a combination of Objects and Strings.
* `[ 'key1', 'key2', ... ]`
Expand Down
7 changes: 6 additions & 1 deletion lib/converter.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface ISharedOptions {
excelBOM?: boolean;

/**
* Specify the keys (as strings) that should be converted
* Specify the keys that should be converted
*
* * If you have a nested object (ie. {info : {name: 'Mike'}}), then set this to ['info.name']
* * If you want all keys to be converted, then specify null or don't specify the option to utilize the default.
Expand Down Expand Up @@ -87,6 +87,11 @@ export interface IFullOptions extends ISharedOptions {
* @default false
*/
useDateIso8601Format?: boolean;

/**
* Specify the keys that should be excluded from the output.
*/
excludeKeys?: string[];
}

export function json2csv(data: object[],
Expand Down
17 changes: 16 additions & 1 deletion lib/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ const Json2Csv = function(options) {
}, 0);
}

/**
* If so specified, this filters the detected key paths to exclude any keys that have been specified
* @param keyPaths {Array<String>}
* @returns {Array<String>} filtered key paths
*/
function filterExcludedKeys(keyPaths) {
if (options.excludeKeys) {
return keyPaths.filter((keyPath) => !options.excludeKeys.includes(keyPath));
}

return keyPaths;
}

/**
* If so specified, this sorts the header field names alphabetically
* @param fieldNames {Array<String>}
Expand Down Expand Up @@ -157,11 +170,13 @@ const Json2Csv = function(options) {

if (options.keys && !options.unwindArrays) {
return Promise.resolve(options.keys)
.then(filterExcludedKeys)
.then(sortHeaderFields);
}

return getFieldNameList(data)
.then(processSchemas)
.then(filterExcludedKeys)
.then(sortHeaderFields);
}

Expand Down Expand Up @@ -202,7 +217,7 @@ const Json2Csv = function(options) {

// If keys were provided, set the headerFields to the provided keys:
if (options.keys) {
params.headerFields = options.keys;
params.headerFields = filterExcludedKeys(options.keys);
}
return params;
});
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
"name": "json-2-csv",
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
"version": "3.9.0",
"version": "3.10.0",
"homepage": "https://mrodrig.github.io/json-2-csv",
"repository": {
"type": "git",
Expand Down
50 changes: 50 additions & 0 deletions test/json2csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,56 @@ function runTests(jsonTestData, csvTestData) {
]
});
});

it('should exclude specified keys from the output', (done) => {
// Change the features array to be empty
const updatedCsvPerOptions = csvTestData.arrayObjects.replace('features.cons,', '')
.replace('"[""cost"",""time""]",', '')
.replace(',,,', ',,');

converter.json2csv(jsonTestData.arrayObjects, (err, csv) => {
if (err) done(err);
csv.should.equal(updatedCsvPerOptions);
done();
}, {
expandArrayObjects: true,
keys: ['name', 'features.name', 'features.pros', 'features.cons', 'downloads'],
excludeKeys: ['features.cons']
});
});

it('should exclude specified keys from the output when unwinding arrays', (done) => {
const updatedCsv = csvTestData.unwind.replace(',data.options.name', '')
.replace(/,MacBook (Pro|Air) \d+/g, '')
.replace(/,(Super|Turbo)charger/g, '')
// De-duplicate the lines since the field isn't unwound due to being excluded
.replace('5cf7ca3616c91100018844af,Computers\n', '')
.replace('5cf7ca3616c91100018844bf,Cars\n', '');

converter.json2csv(jsonTestData.unwind, (err, csv) => {
if (err) done(err);
csv.should.equal(updatedCsv);
done();
}, {
unwindArrays: true,
excludeKeys: ['data.options.name', 'data.options']
});
});

it('should exclude specified deeply nested key from the output when unwinding arrays', (done) => {
const updatedCsv = csvTestData.unwind.replace(',data.options.name', '')
.replace(/,MacBook (Pro|Air) \d+/g, '')
.replace(/,(Super|Turbo)charger/g, '');

converter.json2csv(jsonTestData.unwind, (err, csv) => {
if (err) done(err);
csv.should.equal(updatedCsv);
done();
}, {
unwindArrays: true,
excludeKeys: ['data.options.name']
});
});
});
});

Expand Down

0 comments on commit 995ff6b

Please sign in to comment.