# csv2json
csv2json is a Dart package designed to simplify the
process of converting Comma-Separated Values (CSV) data
to JavaScript Object Notation (JSON) format. It provides
a clean and efficient solution for handling CSV data in
Dart applications.
## Supported Platforms
csv2json is compatible with the following platforms:
- Flutter: Works seamlessly with Flutter for building cross-platform mobile, web, and desktop applications.
- Dart: Can be used with standalone Dart projects for server-side scripting, command-line tools, web applications, and more.
- Web: Supports web applications, including both Flutter web projects and standalone Dart web applications.
- Mobile: Compatible with iOS and Android platforms for mobile application development.
- Desktop: Works for desktop applications targeting Windows, macOS, and Linux platforms.
- Server: Suitable for server-side development, including backend services and APIs written in Dart.
## Features
- CSV to JSON Conversion: Convert CSV data to JSON format effortlessly.
- Trimming: Automatically trim spaces from headers and values to ensure clean and consistent JSON output.
- File Support: Read CSV data directly from files and convert it to JSON with ease.
## Installation
To use csv2json in your Dart project, add it as a dependency in your pubspec.yaml file:
```yaml
dependencies:
csv2json: ^1.0.0
```
Then, run flutter pub get
to install the package.
To convert CSV data to JSON, use the csv2json function:
import 'package:csv2json/csv2json.dart';
void main() {
final csvData = 'name,age\nAlice,30\nBob,25';
final jsonData = csv2json(csvData);
print(jsonData);
}
To convert CSV data from a file to JSON, use the csvFileToJson function:
import 'package:csv2json/csv2json.dart';
void main() async {
final filePath = 'path/to/your/csv/file.csv';
final jsonData = await csvFileToJson(filePath);
print(jsonData);
}
To write JSON data to a file, use the writeJsonToFile function:
import 'package:csv2json/csv2json.dart';
void main() async {
final jsonData = [{'name': 'Alice', 'age': '30'}, {'name': 'Bob', 'age': '25'}];
final filePath = 'output.json';
await writeJsonToFile(jsonData, filePath);
print('JSON data written to $filePath');
}
To convert CSV data to JSON and write it to a file, use the csvToJsonFile function:
import 'package:csv2json/csv2json.dart';
void main() async {
final csvData = 'name,age\nAlice,30\nBob,25';
final outputFilePath = 'output.json';
await csvToJsonFile(csvData, outputFilePath);
print('CSV data converted to JSON and written to $outputFilePath');
}
For more detailed information on usage and additional features, refer to the API documentation.
Check out the example directory in the CSV TO JSON for a complete example demonstrating the usage of csv2json package.
This package is licensed under the BSD-3-Clause.