Skip to content

Commit b8a29b3

Browse files
#28 convert .json data that is not an array to properties array
also resolves #8
1 parent 7c6298e commit b8a29b3

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/data.preview.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,6 @@ export class DataPreview {
386386
case '.tsv':
387387
case '.txt':
388388
case '.tab':
389-
case '.json':
390389
data = fs.readFileSync(dataFilePath, 'utf8'); // file encoding to read data as string
391390
break;
392391
case '.xls':
@@ -409,7 +408,16 @@ export class DataPreview {
409408
break;
410409
case '.config':
411410
data = jsonUtils.objectToPropertyArray(
412-
jsonUtils.flattenObject(JSON.parse(fs.readFileSync(dataFilePath, 'utf8')), true)); // preserve parent path
411+
jsonUtils.flattenObject(
412+
JSON.parse(fs.readFileSync(dataFilePath, 'utf8')), true)); // preserve parent path
413+
break;
414+
case '.json':
415+
data = JSON.parse(fs.readFileSync(dataFilePath, 'utf8'));
416+
if (!Array.isArray(data)) {
417+
// convert it to flat object properties array
418+
data = jsonUtils.objectToPropertyArray(
419+
jsonUtils.flattenObject(data, true)); // preserve parent path
420+
}
413421
break;
414422
case '.arrow':
415423
data = this.getArrowData(dataFilePath);

src/utils/json.utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ export function flattenObject (obj: any, preservePath: boolean = false, parentPa
2929
Object.keys(children).forEach(childKey => {
3030
flatObject[`${parentPath}.${childKey}`] = children[childKey];
3131
});
32+
}
33+
else if (Array.isArray(obj[key])) {
34+
3235
} else {
33-
flatObject[key] = obj[key];
36+
flatObject[key] = obj[key].toString();
3437
}
3538
});
3639
return flatObject;

templates/data.view.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,6 @@
259259
// pass through text data for data view to load
260260
tableData = data;
261261
break;
262-
case '.json':
263-
// try to parse JSON array data
264-
tableData = JSON.parse(data);
265-
break;
266262
case '.xls':
267263
case '.xlsb':
268264
case '.xlsx':
@@ -277,6 +273,7 @@
277273
case '.env':
278274
case '.properties':
279275
case '.config':
276+
case '.json':
280277
// pass through loaded data json
281278
tableData = data;
282279
console.log(`data.view:getData(): records count: ${tableData.length}`);

0 commit comments

Comments
 (0)