You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@onelastjedi/json2csv

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@onelastjedi/json2csv - npm Package Compare versions

Comparing version

to
1.0.2

4

package.json
{
"name": "@onelastjedi/json2csv",
"version": "1.0.1",
"description": "JSON to CSV and back parsers",
"version": "1.0.2",
"description": "Parse JSON to CSV and vice versa in its simplest form",
"homepage": "https://json2csv.phon.one",

@@ -6,0 +6,0 @@ "repository": {

@@ -0,7 +1,27 @@

![bundle size](https://img.shields.io/bundlephobia/minzip/@onelastjedi/json2csv)
![version](https://img.shields.io/npm/v/@onelastjedi/json2csv)
![downloads](https://img.shields.io/npm/dm/@onelastjedi/json2csv)
# json2csv
JavaScript library to transform JSON to CSV and back.
JavaScript library to convert JSON to CSV and back. Has no dependencies. Only works with flat objects.
## Installing
## Installation
If you use npm, `npm install @onelastjedi/json2csv`. You can also download the [latest release on GitHub](https://github.com/onelastjedi/json2csv/releases/latest).
### Use
```js
import parse from '@onelastjedi/json2csv'
const csv = 'name,developer,age\Mary,true,25'
const json = { name: 'John', developer: false, age: 30 }
parse.csv2json(csv) // [{ name: 'Mary', developer: true, age: 25}]
parse.json2csv(json) // name,developer,age\nJohn,false,30
```
### License
[MIT](LICENSE)

@@ -1,12 +0,23 @@

const toPrimitive = require('./toPrimitive')
module.exports = function csv2json (csv) {
const res = []
const arr = csv.split('\n')
const head = arr.slice(0, 1).join().split(',')
const tail = arr.slice(1, arr.length).map(x => x.split(','))
/** @const {function} */
var toPrimitive = require('./toPrimitive')
for (let i = 0; i < tail.length; ++i) {
const f = {}
/**
* Convert csv to json
*
* @param {string} csv - Valid csv string
* @return {array}
*
* @example
*/
function csv2json (csv) {
var res = [],
arr = csv.split('\n'),
head = arr.slice(0, 1).join().split(','),
tail = arr.slice(1, arr.length).map(x => x.split(','))
;
for (let n = 0; n < tail[i].length; ++n) {
for (var i = 0; i < tail.length; ++i) {
var f = {}
for (var n = 0; n < tail[i].length; ++n) {
f[head[n]] = toPrimitive(tail[i][n])

@@ -20,1 +31,3 @@ }

}
module.exports = csv2json

@@ -1,2 +0,12 @@

module.exports = function json2csv (data) {
module.exports = json2csv
/**
* Convert json2csv
*
* @param {*} data - Valid JS object
* @return {string} CSV string
*
* @example
*/
function json2csv (data) {
if (

@@ -9,7 +19,8 @@ !data || data.length === 0 ||

const arr = Array.isArray(data) ? data : [data]
let fields
const columns = []
var arr = Array.isArray(data) ? data : [data]
var fields,
columns = []
;
for (let i = 0; i < arr.length; ++i) {
for (var i = 0; i < arr.length; ++i) {
fields = Object.keys(arr[i])

@@ -16,0 +27,0 @@ columns.push(Object.values(arr[i]))

@@ -1,2 +0,15 @@

module.exports = function toPrimitive (x) {
module.exports = toPrimitive
/**
* Convert string to primitives
*
* @param {string} x - Any string
* @return {*} Detected primitive type
*
* @example
*
* toPrimitive('John') -> 'John'
* toPrimitive('true') -> true
* toPrimitive('2590') -> 2590
*/
function toPrimitive (x) {
if (!x) return ''

@@ -3,0 +16,0 @@ if (parseInt(x)) return parseInt(x)