All in one JSON to CSV and CSV to JSON package
npm install @ideadesignmedia/json-csv
const { csv, json, convertFormat } = require('@ideadesignmedia/json-csv')
``
$3
---
csv(JSON, fields) => CSV as String
example:
`
let object = [
{"name": "Sam", "height": "6'2\""},
{"name": "Jim", "height": "7'3\""}
]
let result = csv(object, ['height'])
console.log(result)
/*
"height"
"6'2"""
"7'3"""
*/
`
$3
---
json(CSV as String, fields) => JSON Object
Returns a promise
example:
`
let string = "name","height"
json(string, ['height']).then(result => {
console.log(result)
/*
[
{"height": "6'2\""},
{"height": "7'3\""}
]
*/
})
`
###convertFormat()
---
convertFormat(path, fields) => ConvertedDocument
Provide the path to a csv or json file and get the result as the other format
example:
`
let doc = './document.csv'
convertFormat(doc).then(r => {
console.log(r)
/*
document as a json array of objects
*/
})
doc = './document.json'
convertFormat(doc).then(r => {
console.log(r)
/*
CSV string of that json document
*/
})
``