Skip to content

Commit

Permalink
auto find array data
Browse files Browse the repository at this point in the history
  • Loading branch information
yesoreyeram committed May 25, 2021
1 parent 9f2eec4 commit c3d8192
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/app/parsers/JSONParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,30 @@ import { getColumnsFromObjectArray } from './utils';
export class JSONParser extends InfinityParser {
constructor(JSONResponse: object, target: InfinityQuery, endTime?: Date) {
super(target);
const jsonResponse = this.formatInput(JSONResponse);
let jsonResponse = this.formatInput(JSONResponse);
if (!(Array.isArray(jsonResponse) || (this.target.json_options && this.target.json_options.root_is_not_array))) {
jsonResponse = this.findArrayData(jsonResponse);
}
if (Array.isArray(jsonResponse) || (target.json_options && target.json_options.root_is_not_array)) {
this.constructTableData(jsonResponse as any[]);
this.constructTimeSeriesData(jsonResponse, endTime);
} else {
this.constructSingleTableData(jsonResponse);
}
}
private findArrayData(input: any): object {
const arrayItems: any[] = Object.keys(input)
.filter((key: string) => {
return Array.isArray(input[key]);
})
.map(key => {
return input[key];
});
if (arrayItems.length > 0) {
return arrayItems[0];
}
return input;
}
private formatInput(JSONResponse: object) {
if (typeof JSONResponse === 'string') {
JSONResponse = JSON.parse(JSONResponse);
Expand Down

0 comments on commit c3d8192

Please sign in to comment.