Skip to content

Commit 791bd01

Browse files
authored
Merge pull request #237 from mrodrig/235
Add expandNestedObjects option - fixes #235
2 parents fd6276f + 94a96cb commit 791bd01

File tree

9 files changed

+45
-4
lines changed

9 files changed

+45
-4
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,27 @@ Returns a `Promise` that resolves with the CSV `string` or rejects with an `Erro
6767
* `excludeKeys` - Array - Specify the keys that should be excluded from the output.
6868
* Default: `[]`
6969
* Note: When used with `unwindArrays`, arrays present at excluded key paths will not be unwound.
70+
* `expandNestedObjects` - Boolean - Should nested objects be deep-converted to CSV?
71+
* Default: `true`
72+
* Example:
73+
```json
74+
[
75+
{
76+
"make": "Nissan",
77+
"model": "Murano",
78+
"year": 2013,
79+
"specifications": {
80+
"mileage": 7106,
81+
"trim": "S AWD"
82+
}
83+
}
84+
]
85+
```
86+
* `true` uses the following keys:
87+
* `['make', 'model', 'year', 'specifications.mileage', 'specifications.trim']`
88+
* `false` uses the following keys:
89+
* `['make', 'model', 'year', 'specifications']`
90+
* Note: This may result in CSV output that does not map back exactly to the original JSON.
7091
* `expandArrayObjects` - Boolean - Should objects in array values be deep-converted to CSV?
7192
* Default: `false`
7293
* Example:

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
},
66
"name": "json-2-csv",
77
"description": "A JSON to CSV and CSV to JSON converter that natively supports sub-documents and auto-generates the CSV heading.",
8-
"version": "4.0.1",
8+
"version": "4.1.0",
99
"homepage": "https://mrodrig.github.io/json-2-csv",
1010
"repository": {
1111
"type": "git",

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const defaultJson2CsvOptions: DefaultJson2CsvOptions = {
2727
emptyFieldValue: undefined,
2828
excelBOM: false,
2929
excludeKeys: [],
30+
expandNestedObjects: true,
3031
expandArrayObjects: false,
3132
prependHeader : true,
3233
preventCsvInjection: false,

src/json2csv.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
1212
customValueParser = options.parseValue && typeof options.parseValue === 'function' ? options.parseValue : null,
1313
expandingWithoutUnwinding = options.expandArrayObjects && !options.unwindArrays,
1414
deeksOptions = {
15+
expandNestedObjects: options.expandNestedObjects,
1516
expandArrayObjects: expandingWithoutUnwinding,
1617
ignoreEmptyArraysWhenExpanding: expandingWithoutUnwinding,
1718
escapeNestedDots: true

src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ export interface Json2CsvOptions extends SharedConverterOptions {
9898
*/
9999
emptyFieldValue?: unknown;
100100

101+
/**
102+
* Should nested objects be deep-converted to CSV
103+
* @default true
104+
*/
105+
expandNestedObjects?: boolean;
106+
101107
/**
102108
* Should objects in array values be deep-converted to CSV
103109
* @default false

test/config/testCsvFilesList.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ const csvFileConfig = [
4747
{key: 'nestedDotKeysWithArrayExpandedUnwound', file: '../data/csv/nestedDotKeysWithArrayExpandedUnwound.csv'},
4848
{key: 'emptyColumns', file: '../data/csv/emptyColumns.csv'},
4949
{key: 'quotedFieldWithNewline', file: '../data/csv/quotedFieldWithNewline.csv'},
50-
{key: 'falsyValues', file: '../data/csv/falsyValues.csv'}
50+
{key: 'falsyValues', file: '../data/csv/falsyValues.csv'},
51+
{key: 'nestedNotUnwoundObjects', file: '../data/csv/nestedNotUnwoundObjects.csv'},
5152
];
5253

5354
function readCsvFile(filePath: string) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
level1
2+
"{""level2"":{""a"":1,""level3"":{""level"":""zero""}}}"
3+
"{""level2"":{""a"":2,""level3"":{""level"":4}}}"

test/json2csv.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,14 @@ export function runTests() {
616616

617617
assert.equal(csv, expectedCsv);
618618
});
619+
620+
it('should respect the expandNestedObjects option being set to false', async () => {
621+
const csv = await json2csv(jsonTestData.nested, {
622+
expandNestedObjects: false,
623+
});
624+
625+
assert.equal(csv, csvTestData.nestedNotUnwoundObjects);
626+
});
619627
});
620628
});
621629

0 commit comments

Comments
 (0)