fromJson static method

Future<ImportResult> fromJson(
  1. String filePath, {
  2. String? schemaName,
})

Imports from a JSON file

Implementation

static Future<ImportResult> fromJson(
  String filePath, {
  String? schemaName,
}) async {
  try {
    final file = File(filePath);
    if (!await file.exists()) {
      return ImportResult(
        schema: _createEmptySchema(schemaName ?? 'ImportedData'),
        data: [],
        rowCount: 0,
        error: 'File not found: $filePath',
      );
    }

    final content = await file.readAsString();
    return fromJsonString(
      content,
      schemaName: schemaName ?? _extractFileName(filePath),
    );
  } catch (e) {
    return ImportResult(
      schema: _createEmptySchema(schemaName ?? 'ImportedData'),
      data: [],
      rowCount: 0,
      error: 'Error reading the file: $e',
    );
  }
}