npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-convert-csv

v1.0.3

Published

convert json to csv

Downloads

4

Readme

json-convert-csv

npm GitHub stars label

json convert to csv or csv convert to json.

json 数据转换为 csv 表格。

[
  {
    "name": "小白",
    "sex": "男",
    "age": "18",
    "tel": "1383794XXXX"
    "address": "广东省 广州市"
  },
  {
    "name": "小红",
    "sex": "女",
    "age": "14",
    "tel": "1337893XXXX",
    "address": "广东省 广州市"
  }
]

| 姓名 | 性别 | 年龄 | 电话 | 地址 | | ---- | :--: | :--: | ----------- | ------- | | 小白 | 男 | 18 | 1383794XXXX | 广东省 广州市 | | 小红 | 女 | 14 | 1337893XXXX | 广东省 广州市 |

Usage

npm install -S json-convert-csv
  • json to csv
const fs = require('fs')
const CSVStream = require('json-convert-csv').CSVStream
const csvSteam = new CSVStream()

const json = fs.createReadStream('path-to-json-file')
const csv = fs.createWriteStream('path-to-csv-file')

json.pipe(csvSteam).pipe(csv)
  • csv to json
const fs = require('fs')
const JSONStream = require('json-convert-csv').JSONStream
const jsonStream = new JSONStream()

const csv = fs.createReadStream('path-to-csv-file')
const JSONFile = fs.createWriteStream('new-path-to-json-file')

csv.pipe(jsonStream).pipe(JSONFile)

Options

new CSVStream([options])

  • options 配置对象
    • header <string[]> 数据的字段名。['name', 'age', 'tel']
    • headerDisplay <string[]> 数据的显示名称。['姓名', '年龄', '电话']

Event

new JSONStream([options])

  • options 配置对象
    • fieldNames <string[]> 字段名数组。
    • lineEnding <RegExp> 行结束符。默认为/\n/ 。也可设置为 /\r\n/
    • separator <RegExp> 分隔符。默认为 /[,]/。可设置为 /\t//[ ]/
    • ignoreFirstLine <boolean> 是否忽略首行。默认 true

Event

  • line 当解析每一行时触发。

parse

参数

| 名称 | 类型 | 描述 | | ----- | ------ | -------- | | obj | Object | 解析的每个对象 | | index | Number | 第几个解析的对象 |

convert

参数

| 名称 | 类型 | 描述 | | ---------- | ------ | -------- | | convertStr | String | 格式化的字符串。 | | index | Number | 第几个解析的对象 |

error

参数

| 名称 | 类型 | 描述 | | ----- | ----- | ---------- | | error | Error | 转换过程出现的错误。 |

line

参数

| 名称 | 类型 | 描述 | | ---- | ------ | -------------- | | obj | Object | csv 每一行触析出的对象。 |

Simple

const convert = new Csv({
  header: ['name', 'age', 'tel'],
  headerDisplay: ['姓名', '年龄', '电话']
})
/*
<<< Input JSON
[
  {
    "name": "小白",
    "age": "18",
    "tel": "1383794XXXX"
  },
  {
    "name": "小红",
    "age": "14",
    "tel": "1337893XXXX"
  }
]
>>> Output CSV
*/

| 姓名 | 年龄 | 电话 | | ---- | ---- | ----------- | | 小白 | 18 | 1383794XXXX | | 小红 | 14 | 1337893XXXX |