0.0.9 • Published 9 years ago

csv-to-json-lookup v0.0.9

Weekly downloads
27
License
-
Repository
github
Last release
9 years ago

csv-to-json

For node 4.2.4 and higher (uses ES6 functionality).

Turn csv file into JSON for quick key value lookups.

Typical use case: you have a csv export from a legacy system and it's few enough rows that it will fit in memory easily. You want to do a lookup on the data by one of they columns (generally an id column).

TODO add ability to specify just the value fields you want and maybe some other options like always make value JSON. Header row or not. Etc.

Install

npm install csv-to-json-lookup

Use

const c2j = require('csv-to-json-lookup');

c2j('./test/two.csv', 'name', (error, json) => {
    if (error) throw error;
    console.log(JSON.stringify(json));
});

c2j('./test/four.csv', 'name', (error, json) => {
    if (error) throw error;
    console.log(JSON.stringify(json));
});

Output should be as follows.

{"sally":"38","bill":"18","steve":"75","mary":"1"}

{"sally":{"age":"3","height":"4","weight":"5"},"bill":{"age":"6","height":"7","weight":"8"},"steve":{"age":"9","height":"10","weight":"11"},"mary":{"age":"12","height":"13","weight":"14"}}

Given these two CSV files.

two.csv

name,age
sally,38
bill,18
steve,75
mary,1

four.csv

name,age,height,weight
sally,3,4,5
bill,6,7,8
steve,9,10,11
mary,12,13,14
0.0.9

9 years ago

0.0.8

9 years ago

0.0.7

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago