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

csv2tree

v1.0.0

Published

Converts flat csv to nested parent/child array in JSON format

Downloads

3

Readme

#About

csv2tree is a simple module for converting comma separated values containing parent child relationships into nested hierarchical JSON arrays. This module was created to help analyze data containing parent child relationships represented in a flat format.

##Example Input

id | parent_id | name
--- | ---------- | ---- 1 | 1 | Jim 2 | 1 | Jeff 3 | 2 | John 4 | 4 | Jason 5 | 4 | Jacob 6 | 2 | Jack 7 | 6 | James

In the example data above, each record has an id, and a parent_id. Each parent_id is the parent record's id. For instance, Jim is Jeff's parent because Jim's id is 1, and the parent_id for Jeff is 1.

In csv format, the data is represented as follows:

id,parent_id,name
1,,Jim
2,1,Jeff
3,2,John
4,1,Jason
5,4,Jacob
6,2,Jack
7,6,James

This data is typical of the query output of a table which contains a 1:n parent child relationship where the parent record id refers to the id or primary key of another record in the same table. The data would be much easier to understand visually if it was represented as a nested tree.

##Example Output

[
	{
		"id": "1",
		"parent_id": "",
		"name": "Jim",
		"children": [
			{
				"id": "2",
				"parent_id": "1",
				"name": "Jeff",
				"children": [
					{
						"id": "3",
						"parent_id": "2",
						"name": "John",
						"children": []
					},
					{
						"id": "6",
						"parent_id": "2",
						"name": "Jack",
						"children": [
							{
								"id": "7",
								"parent_id": "6",
								"name": "James",
								"children": []
							}
						]
					}
				]
			},
			{
				"id": "4",
				"parent_id": "1",
				"name": "Jason",
				"children": [
					{
						"id": "5",
						"parent_id": "4",
						"name": "Jacob",
						"children": []
					}
				]
			}
		]
	}
]

This representation of the data is also easily converted to something like a unix style directory tree using a module such as treeify.

##Example Usage

// Read csv data from a file and convert to a JSON tree

var fs = require('fs')
var csv2tree = require('csv2tree')

var data = '~/data.csv'      // Path to file
var id = 'id'                // Name of record ID column in csv data
var parent_id = 'parent_id'  // Name of parent record ID column in csv data

fs.readFile(data, function (csv) {
  csv2tree(csv.toString(), id, parent_id, function (tree) {
    console.log(tree)
  })
})

The csv2tree function requires 4 arguments which must be passed to csv2tree in the correct order. The first argument is the csv data. The second argument is the name of the record id column as it appears in the csv header (you will need to append headers if your csv does not include them). The third argument is the the record id of each record's parent record. The last argument is a callback function, which returns the nested JSON tree.