data-flip
v1.0.1
Published
A generic utility library for converting between JSON, CSV, TXT, and XLSX formats with auto-detection
Maintainers
Readme
Data Format Converter
A lightweight, generic Node.js utility library for converting between JSON, CSV, TXT, and XLSX formats with automatic delimiter detection.
Features
- ✅ JSON ↔ CSV conversion
- ✅ JSON ↔ TXT conversion
- ✅ CSV ↔ TXT conversion
- ✅ XLSX → CSV conversion
- ✅ Auto-detect delimiters (comma, tab, etc.)
- ✅ Simple API - Easy to use
- ✅ Zero configuration - Works out of the box
- ✅ Flexible options - Customize as needed
Installation
npm install data-flipUsage
Import the library
const DataFormatConverter = require('data-flip');JSON to CSV
const jsonData = [
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Jane', age: 25, city: 'London' }
];
const csv = DataFormatConverter.jsonToCsv(jsonData);
console.log(csv);
// Output:
// name,age,city
// John,30,New York
// Jane,25,LondonCSV to JSON
const csvString = `name,age,city
John,30,New York
Jane,25,London`;
const json = DataFormatConverter.csvToJson(csvString);
console.log(json);
// Output:
// [
// { name: 'John', age: '30', city: 'New York' },
// { name: 'Jane', age: '25', city: 'London' }
// ]JSON to TXT (Tab-delimited)
const jsonData = [
{ name: 'John', age: 30, city: 'New York' },
{ name: 'Jane', age: 25, city: 'London' }
];
const txt = DataFormatConverter.jsonToTxt(jsonData);
console.log(txt);
// Output:
// name age city
// John 30 New York
// Jane 25 LondonTXT to JSON (Auto-detect delimiter)
const txtString = `name\tage\tcity
John\t30\tNew York
Jane\t25\tLondon`;
const json = DataFormatConverter.txtToJson(txtString);
console.log(json);
// Output:
// [
// { name: 'John', age: '30', city: 'New York' },
// { name: 'Jane', age: '25', city: 'London' }
// ]TXT to CSV
const txtString = `name\tage\tcity
John\t30\tNew York
Jane\t25\tLondon`;
const csv = DataFormatConverter.txtToCsv(txtString);
console.log(csv);
// Output:
// name,age,city
// John,30,New York
// Jane,25,LondonCSV to TXT
const csvString = `name,age,city
John,30,New York
Jane,25,London`;
const txt = DataFormatConverter.csvToTxt(csvString);
console.log(txt);
// Output:
// name age city
// John 30 New York
// Jane 25 LondonXLSX to CSV
// Convert from file path
const csv = DataFormatConverter.xlsxToCsv('./data.xlsx');
console.log(csv);
// Output:
// name,age,city
// John,30,New York
// Jane,25,London
// Convert specific sheet by name
const csv2 = DataFormatConverter.xlsxToCsv('./data.xlsx', {
sheetName: 'Sheet2'
});
// Convert specific sheet by index
const csv3 = DataFormatConverter.xlsxToCsv('./data.xlsx', {
sheetIndex: 1
});
// Convert from Buffer (useful for uploaded files)
const fs = require('fs');
const buffer = fs.readFileSync('./data.xlsx');
const csv4 = DataFormatConverter.xlsxToCsv(buffer);Auto-detect and Convert to JSON
// Works with CSV
const csvString = `name,age,city
John,30,New York`;
const json1 = DataFormatConverter.autoToJson(csvString);
// Works with tab-delimited
const txtString = `name age city
John 30 New York`;
const json2 = DataFormatConverter.autoToJson(txtString);
// Works with JSON string
const jsonString = '[{"name":"John","age":30}]';
const json3 = DataFormatConverter.autoToJson(jsonString);API Reference
jsonToCsv(data, options)
Convert JSON to CSV format.
Parameters:
data(Array|Object): JSON data to convertoptions(Object): Optional configurationfields(Array): Field names to include (auto-detected if not provided)
Returns: CSV string
csvToJson(csvString, options)
Convert CSV to JSON format.
Parameters:
csvString(String): CSV string to convertoptions(Object): Optional configurationdelimiter(String): Delimiter character (auto-detected if not provided)
Returns: Array of objects
jsonToTxt(data, options)
Convert JSON to delimited text format.
Parameters:
data(Array|Object): JSON data to convertoptions(Object): Optional configurationdelimiter(String): Delimiter character (default: tab\t)includeHeaders(Boolean): Include headers (default:true)
Returns: Delimited text string
txtToJson(txtString, options)
Convert delimited text to JSON format.
Parameters:
txtString(String): Delimited text string to convertoptions(Object): Optional configurationdelimiter(String): Delimiter character (auto-detected if not provided)
Returns: Array of objects
txtToCsv(txtString, options)
Convert delimited text to CSV format.
Parameters:
txtString(String): Delimited text string to convertoptions(Object): Optional configurationinputDelimiter(String): Input delimiter (auto-detected if not provided)outputDelimiter(String): Output delimiter (default: comma,)
Returns: CSV string
csvToTxt(csvString, options)
Convert CSV to delimited text format.
Parameters:
csvString(String): CSV string to convertoptions(Object): Optional configurationinputDelimiter(String): Input delimiter (auto-detected if not provided)outputDelimiter(String): Output delimiter (default: tab\t)
Returns: Delimited text string
autoToJson(data, options)
Auto-detect format and convert to JSON.
Parameters:
data(String): Input data stringoptions(Object): Optional configurationdelimiter(String): Delimiter character (auto-detected if not provided)
Returns: Array of objects
xlsxToCsv(xlsxData, options)
Convert XLSX file to CSV format.
Parameters:
xlsxData(String|Buffer): Path to XLSX file or Buffer containing XLSX dataoptions(Object): Optional configurationsheetName(String): Sheet name to convert (default: first sheet)sheetIndex(Number): Sheet index to convert (default: 0)
Returns: CSV string
Advanced Options
Custom Delimiters
// Use semicolon as delimiter
const csv = DataFormatConverter.jsonToCsv(data, { delimiter: ';' });
// Convert pipe-delimited to JSON
const json = DataFormatConverter.txtToJson(txtString, { delimiter: '|' });Specify Fields
// Only include specific fields
const csv = DataFormatConverter.jsonToCsv(data, {
fields: ['name', 'age']
});Without Headers
// Generate text without headers
const txt = DataFormatConverter.jsonToTxt(data, {
includeHeaders: false
});Error Handling
try {
const json = DataFormatConverter.csvToJson(csvString);
} catch (error) {
console.error('Conversion failed:', error.message);
}License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
For issues and questions, please open an issue on GitHub.
