Skip to content

Instantly share code, notes, and snippets.

@code-simple
Last active September 29, 2021 19:59
Show Gist options
  • Save code-simple/92cc89153b7412dbecc113a0ade0bc09 to your computer and use it in GitHub Desktop.
Save code-simple/92cc89153b7412dbecc113a0ade0bc09 to your computer and use it in GitHub Desktop.
# Very Simplest Way to Create CSV Out of JSON, List Data, or Almost anything \n In this Example We will Make CSV of a JSON that has List<Map<String, dynamic>> Data
import 'dart:convert';
import 'dart:io';
// This Will extract data from JSON file and Create CSV file.
// people.json DownloadLink : https://gist.githubusercontent.com/code-simple/dff6ec9eaa0816131d7273578f38c3e1/raw/6c8e8b9bb36003c2f3b9ca3cefc8a4bcfe2910eb/people.json
// new.csv DownloadLink : https://gist.githubusercontent.com/code-simple/7cae32a464f8a9995a6e510528edc576/raw/d31870b6dac5781ac4ee1d203e66334320bfec96/new.csv
void main() {
// Read CSV [Simple CSV that has first row header, and data in next rows]
List<Map<dynamic, dynamic>> readCSV(String fileName) {
// // Reading CSV
File r = File(fileName);
// CSV converted to List splitted by new line
var csvInput = (r.readAsStringSync()).split('\n');
List<Map<dynamic, dynamic>> csvData = [];
// Prepare header Of CSV
var header = [];
for (var i = 0; i < csvInput[0].split(',').length - 1; i++) {
header.add(csvInput[0].split(',')[i]);
}
// Extract Data to its relevant Header
for (int i = 0; i < csvInput.length - 1; i++) {
csvData.add({}); // We add
// If CSV File Row is Empty, i.e when reach at bottom return Null
if (csvInput[i].isNotEmpty) {
for (var j = 0; j < header.length; j++) {
csvData[i][header[j]] = csvInput[i].split(',')[j];
}
}
}
return csvData;
}
///-------------------- [Write to CSV] ------------------
/// Description : This Method will write Any JSON file to CSV , plus it will also accept List<dynamic>
// Either Provide JSON file Path or List<dynamic> , jsonFileName or listDynamic either one.
writeCSV(
{String jsonFileName = '',
String outputCSV = 'outputCSV.csv',
List<dynamic> listDynamic = const []}) {
var jsonData, f;
// Check Which Method selected, Either File or List<dynamic>
if (jsonFileName.isEmpty) {
jsonData = listDynamic;
} else {
File f = File(jsonFileName);
jsonData = jsonDecode(f.readAsStringSync());
}
//Prepare header of columns
var header = [];
for (var k in jsonData[0].entries) {
header.add(k.key);
}
String data = '';
for (var i = 0; i < header.length; i++) {
data += header[i] + ',';
}
// Removes Comma at last of header and adding newline
data = data.substring(0, data.length - 1) + '\n';
for (var i = 0; i < jsonData.length; i++) {
for (var j = 0; j < header.length; j++) {
var increment = '${jsonData[i][header[j]]},';
data += increment;
}
//Removes Comma at last of row and add newline
data = data.substring(0, data.length - 1) + '\n';
}
//When complete then remove last empty line
data = data.substring(0, data.length - 1);
print(data);
File csv = File(outputCSV);
csv.writeAsStringSync(data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment