1.1.2 • Published 6 years ago

json-2-csv-ts v1.1.2

Weekly downloads
46
License
ISC
Repository
github
Last release
6 years ago

json-2-csv-ts

Converts json to csv

How to use

Install

$ npm install json-2-csv-ts --save
import { json2csv } from 'json-2-csv-ts';
const myData = {
    id:"id",
    name:"name",
    comments:[
        "first",
        "second",
        "third"
    ]
}

try {
  var result = json2csv(myData)
  console.log(result);
} catch (err) {
  console.error(err);
}

Options

  • data - Required Array or Object
  • options

Example 1

import { json2csv } from 'json-2-csv-ts';
const myData = {
    id:"id",
    name:"name",
    comments:[
        "first",
        "second",
        "third"
    ]
}

try {
  var result = json2csv(myData,{
      expandArray:true
  })
  console.log(result);
} catch (err) {
  console.error(err);
}

The result should be

id,name,comments
"id",name,"first"
,,"second"
,,"third"

The csvFile looks like

idnamecomments
idnamefirst
second
third

Example 2

import { json2csv } from 'json-2-csv-ts';
const myData = {
    id:"id",
    name:"name",
    comments:[
        "first",
        "second",
        "third"
    ]
}

try {
  var result = json2csv(myData,{
      expandArray:false
  })
  console.log(result);
} catch (err) {
  console.error(err);
}

The result should be

id,name,comments
"id",name,"first,second,third"

The csvFile looks like

idnamecomments
idnamefirst,second,third

Example 3

import { json2csv } from 'json-2-csv-ts';
const myData = [
  {
    id:"1",
    name:"test"
  },
  {
    id:"2",
    des:"desc"
  }
]
    

try {
  var result = json2csv(myData,{
      expandArray:true
  })
  console.log(result);
} catch (err) {
  console.error(err);
}

The result should be

id,name,des
"1","test",
"2",,"desc"

The csvFile looks like

idnamedes
1test
2desc
1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.5

6 years ago