1.1.1 • Published 4 years ago
jsv-convert v1.1.1
jsv-convert
Converts Javascript object to csv file
Installation
npm i jsv-convert
🏆 Contributing
Please read CONTRIBUTING.md for information on how to contribute to the project.
💼 Code of Conduct
We want to facilitate a healthy and constructive community behavior by adopting and enforcing our code of conduct.
Please adhere towards our Code of Conduct.
❤️ Thanks to our awesome contributors.
🌟 STAR AND FORK THE REPOSITORY FOR YOUR FUTURE REFERENCE.🌟
In Nodejs
const {prepareTable} = require('../index')
const params = [{laguage: 'Javascript',rating: 10},{laguage: 'Dart',rating: 10}]
const data = prepareTable(params) // pass in a array of objects
console.log(data)
Usage
This is a utility library for converting JSON data to CSV format
Steps to be followed :-
- Import
FileCoverter
class - Create an object of
FileConverter
which takes path of the file to be converted as parameter - invoke
generateCsvformat()
method generateCsvformat()
method returns csv data as a stringgenerateCsvFile(path: string)
takes path of file as argument and writes conveted data to file
import {FileConverter} from 'jsv-convert'
import path from 'path'
const fc = new FileConverter(path.join(__dirname,'data.json')) // takes path of the file as a parameter
console.log(fc.generateCsvFormat())
fc.generateCsvFile(path)
Examples:-
JSON
const data = [
{"language": "javascript", "rating": 5},
{"language": "java", "rating": 5}
]
CSV
const data = `
language, rating
javascript, 5
java, 5
`
- JSON
const data = [
{"fruit": "Apple","size": "Large","color": "Red"},
{"fruit": "Mango","size": "Small","color": "Yellow"},
{"fruit": "Tangerine","size": "Large","color": "Orange"}
]
CSV
const data = `
fruit, size, color
Apple, Large,Red
Mango, Small, Yellow
Tangerine, Large, Orange
`
JSON
const data={
"firstName": "Rack",
"lastName": "Jackon",
"gender": "man",
"age": 24,
"address": {
"streetAddress": "126",
"city": "San Jone",
"state": "CA",
"postalCode": "394221"
},
"phoneNumbers": [
{ "type": "home", "number": "7383627627" }
]
}
CSV
const data = `
firstName, lastName, gender, age, address_streetAddress, address_city,
address_state, address_postalCode, phoneNumbers_type, phoneNumbers_number
Rack, Jackon, man, 24, 126, San Jone, CA, 394221, home, 7383627627
`
Outputs the data in a array suitable for csv file format
language | rating |
---|---|
Javascript | 10 |
Dart | 10 |