Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add arrayIndexesAsKeys option for #207 #250

Merged
merged 4 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Returns the CSV `string` or rejects with an `Error` if there was an issue.

* `array` - An array of JSON documents to be converted to CSV.
* `options` - (Optional) A JSON document specifying any of the following key value pairs:
* `arrayIndexesAsKeys` - Boolean - Should array indexes be included in the generated keys?
* Default: `false`
* Note: This provides a more accurate representation of the JSON in the returned CSV, but may be less human readable. See [#207](https://github.com/mrodrig/json-2-csv/issues/207) for more details.
* `checkSchemaDifferences` - Boolean - Should all documents have the same schema?
* Default: `false`
* Note: An error will be thrown if some documents have differing schemas when this is set to `true`.
Expand Down
16 changes: 8 additions & 8 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
"cli"
],
"dependencies": {
"deeks": "3.0.2",
"doc-path": "4.0.2"
"deeks": "3.1.0",
"doc-path": "4.1.0"
},
"devDependencies": {
"@types/mocha": "10.0.1",
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const errors = {
};

export const defaultJson2CsvOptions: DefaultJson2CsvOptions = {
arrayIndexesAsKeys: false,
checkSchemaDifferences: false,
delimiter : {
field : ',',
Expand Down
1 change: 1 addition & 0 deletions src/json2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
customValueParser = options.parseValue && typeof options.parseValue === 'function' ? options.parseValue : null,
expandingWithoutUnwinding = options.expandArrayObjects && !options.unwindArrays,
deeksOptions = {
arrayIndexesAsKeys: options.arrayIndexesAsKeys,
expandNestedObjects: options.expandNestedObjects,
expandArrayObjects: expandingWithoutUnwinding,
ignoreEmptyArraysWhenExpanding: expandingWithoutUnwinding,
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ export interface Csv2JsonOptions extends Omit<SharedConverterOptions, 'keys'> {
}

export interface Json2CsvOptions extends SharedConverterOptions {
/** Should array indexes be included in the generated keys?
* @default false
*/
arrayIndexesAsKeys?: boolean;

/**
* Should all documents have the same schema?
* @default false
Expand Down
1 change: 1 addition & 0 deletions test/config/testCsvFilesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const csvFileConfig = [
{key: 'newlineWithWrapDelimiters', file: '../data/csv/newlineWithWrapDelimiters.csv'},
{key: 'excludeKeyPattern', file: '../data/csv/excludeKeyPattern.csv'},
{key: 'wildcardMatch', file: '../data/csv/wildcardMatch.csv'},
{key: 'arrayIndexesAsKeys', file: '../data/csv/arrayIndexesAsKeys.csv'},
];

function readCsvFile(filePath: string) {
Expand Down
1 change: 1 addition & 0 deletions test/config/testJsonFilesList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ export default {
newlineWithWrapDelimiters: require('../data/json/newlineWithWrapDelimiters'),
excludeKeyPattern: require('../data/json/excludeKeyPattern'),
wildcardMatch: require('../data/json/wildcardMatch.json'),
arrayIndexesAsKeys: require('../data/json/arrayIndexesAsKeys.json'),
};
3 changes: 3 additions & 0 deletions test/data/csv/arrayIndexesAsKeys.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test.list.0.a,test.list.0.optionA,test.list.1.a,test.list.1.optionB
1,ac,2,radio
3,cd,4,heat
30 changes: 30 additions & 0 deletions test/data/json/arrayIndexesAsKeys.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[
{
"test": {
"list": [
{
"a": 1,
"optionA": "ac"
},
{
"a": 2,
"optionB": "radio"
}
]
}
},
{
"test": {
"list": [
{
"a": 3,
"optionA": "cd"
},
{
"a": 4,
"optionB": "heat"
}
]
}
}
]
8 changes: 8 additions & 0 deletions test/json2csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,14 @@ export function runTests() {
assert.equal(csv, updatedCsv);
});

// Test case for #207
it('should include the array indexes in CSV key headers if specified via the option', () => {
const csv = json2csv(jsonTestData.arrayIndexesAsKeys, {
arrayIndexesAsKeys: true,
});
assert.equal(csv, csvTestData.arrayIndexesAsKeys);
});

it('should use a custom value parser function when provided', () => {
const updatedCsv = csvTestData.trimmedFields.split('\n');
const textRow = 'Parsed Value,Parsed Value,Parsed Value,Parsed Value,Parsed Value';
Expand Down
Loading