diff --git a/package.json b/package.json index c67fb6c..4614c73 100755 --- a/package.json +++ b/package.json @@ -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": "5.5.5", + "version": "5.5.6", "homepage": "https://mrodrig.github.io/json-2-csv", "repository": { "type": "git", diff --git a/src/json2csv.ts b/src/json2csv.ts index 50ccf24..14501f0 100755 --- a/src/json2csv.ts +++ b/src/json2csv.ts @@ -35,6 +35,11 @@ export const Json2Csv = function(options: FullJson2CsvOptions) { * list of field names. */ function processSchemas(documentSchemas: string[][]) { + // If there are no document schemas then there is nothing to diff and no unqiue fields to get + if (documentSchemas.length === 0) { + return []; + } + // If the user wants to check for the same schema (regardless of schema ordering) if (options.checkSchemaDifferences) { return checkSchemaDifferences(documentSchemas); @@ -53,8 +58,8 @@ export const Json2Csv = function(options: FullJson2CsvOptions) { function checkSchemaDifferences(documentSchemas: string[][]) { // have multiple documents - ensure only one schema (regardless of field ordering) const firstDocSchema = documentSchemas[0], - restOfDocumentSchemas = documentSchemas.slice(1), - schemaDifferences = computeNumberOfSchemaDifferences(firstDocSchema, restOfDocumentSchemas); + restOfDocumentSchemas = documentSchemas.slice(1), + schemaDifferences = computeNumberOfSchemaDifferences(firstDocSchema, restOfDocumentSchemas); // If there are schema inconsistencies, throw a schema not the same error if (schemaDifferences) { @@ -457,7 +462,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) { */ function convert(data: object[]) { // Single document, not an array - if (utils.isObject(data) && !data.length) { + if (!Array.isArray(data)) { data = [data]; // Convert to an array of the given document } diff --git a/test/config/testCsvFilesList.ts b/test/config/testCsvFilesList.ts index 2753c8e..d94010a 100644 --- a/test/config/testCsvFilesList.ts +++ b/test/config/testCsvFilesList.ts @@ -25,6 +25,7 @@ const csvFileConfig = [ {key: 'trimmedHeader', file: '../data/csv/trimmedHeader.csv'}, {key: 'excelBOM', file: '../data/csv/excelBOM.csv'}, {key: 'specifiedKeys', file: '../data/csv/specifiedKeys.csv'}, + {key: 'specifiedKeysNoData', file: '../data/csv/specifiedKeysNoData.csv'}, {key: 'extraLine', file: '../data/csv/extraLine.csv'}, {key: 'noHeader', file: '../data/csv/noHeader.csv'}, {key: 'sortedHeader', file: '../data/csv/sortedHeader.csv'}, diff --git a/test/data/csv/specifiedKeysNoData.csv b/test/data/csv/specifiedKeysNoData.csv new file mode 100644 index 0000000..33e1c20 --- /dev/null +++ b/test/data/csv/specifiedKeysNoData.csv @@ -0,0 +1 @@ +arrayOfStrings,object.subField diff --git a/test/json2csv.ts b/test/json2csv.ts index f4fab8e..1668d00 100644 --- a/test/json2csv.ts +++ b/test/json2csv.ts @@ -293,6 +293,13 @@ export function runTests() { assert.equal(csv, csvTestData.specifiedKeys); }); + it('should only contain a header when given an empty array and the keys option is provided', () => { + const csv = json2csv(jsonTestData.noData, { + keys: ['arrayOfStrings', 'object.subField'] + }); + assert.equal(csv, csvTestData.specifiedKeysNoData); + }); + it('should use the specified empty field value, if provided', () => { jsonTestData.emptyFieldValues[0].number = undefined;