csvToJson static method

String csvToJson(
  1. String csv, {
  2. String delimiter = ",",
  3. bool pretty = false,
})

Implementation

static String csvToJson(String csv, {String delimiter = ",", bool pretty = false}) {
  final List<List<String>> rows = _parseCsv(csv, delimiter);
  if (rows.isEmpty) return "[]";
  final List<String> headers = rows.first;
  final List<Map<String, String>> out = <Map<String, String>>[];
  for (int i = 1; i < rows.length; i++) {
    final Map<String, String> obj = <String, String>{};
    for (int j = 0; j < headers.length; j++) {
      obj[headers[j]] = j < rows[i].length ? rows[i][j] : "";
    }
    out.add(obj);
  }
  return pretty ? const JsonEncoder.withIndent("  ").convert(out) : jsonEncode(out);
}