csv2json 0.0.1
csv2json: ^0.0.1 copied to clipboard
A Dart package for converting CSV data to JSON format.
# 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.
## 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.
Usage #
Converting CSV Data to JSON #
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);
}
Converting CSV File to JSON #
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);
}
Writing JSON Data to File #
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');
}
Converting CSV Data to JSON and Writing to File #
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.
Example #
Check out the example directory in the CSV TO JSON for a complete example demonstrating the usage of csv2json package.
License #
This package is licensed under the BSD-3-Clause.