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

deepjsontocsv

v0.0.5

Published

A TypeScript utility for converting deeply nested JSON objects or arrays into CSV format. This library flattens nested objects and arrays, allowing you to easily generate CSV files from complex JSON structures.

Downloads

11

Readme

deepjsontocsv

A TypeScript utility for converting deeply nested JSON objects or arrays into CSV format. This library flattens nested objects and arrays, allowing you to easily generate CSV files from complex JSON structures.

Features

  • Converts both a single JSON object and an array of JSON objects into CSV.
  • Handles nested objects and arrays, flattening them into a single level.
  • Automatically generates headers based on the JSON structure.
  • Flexible and supports optional or undefined values in JSON.

Installation

You can install the package via npm:

npm install deepjsontocsv

Usage

Basic Example

Here’s an example of how to use the library:

import { jsonToCsv } from 'deepjsontocsv';

const jsonObject = {
  album: "The White Stripes",
  year: 1999,
  US_peak_chart_post: "-",
  main_actor: {
    name: "Jack White",
    birth: "1975",
  },
  actors: [
    {
      name: "Jack White",
      birth: "1975",
    },
  ],
};

const csvResult = jsonToCsv(jsonObject);
console.log(csvResult);

Output:

album,year,US_peak_chart_post,main_actor.name,main_actor.birth,actors[0].name,actors[0].birth
The White Stripes,1999,-,Jack White,1975,Jack White,1975

Working with Arrays

If you have an array of JSON objects, the library will handle it seamlessly:

const jsonArray = [
  {
    album: "The White Stripes",
    year: 1999,
    US_peak_chart_post: "-",
    main_actor: {
      name: "Jack White",
      birth: "1975",
    },
    actors: [
      {
        name: "Jack White",
        birth: "1975",
      },
    ],
  },
  {
    album: "De Stijl",
    year: 2000,
    US_peak_chart_post: "-",
  },
];

const csvResult = jsonToCsv(jsonArray);
console.log(csvResult);

Output:

album,year,US_peak_chart_post,main_actor.name,main_actor.birth,actors[0].name,actors[0].birth
The White Stripes,1999,-,Jack White,1975,Jack White,1975
De Stijl,2000,-,,,,

Handling Undefined or Missing Fields

The library gracefully handles undefined or missing fields, ensuring the output remains valid.

typescript
Copy code
const jsonArrayWithMissingFields = [
  {
    album: "De Stijl",
    year: 2000,
    US_peak_chart_post: "-",
  },
];

const csvResult = jsonToCsv(jsonArrayWithMissingFields);
console.log(csvResult);

Output:

album,year,US_peak_chart_post
De Stijl,2000,-

API

jsonToCsv(json: JsonObject | JsonObject[]): string

  • Parameters:
    • json: A single JSON object or an array of JSON objects to be converted into CSV.
  • Returns:
    • A string in CSV format representing the input JSON data.

Type Definitions

The following types are used by the library:

type JsonValue = string | number | boolean | null | undefined | JsonObject | JsonValue[];
interface JsonObject {
    [key: string]: JsonValue;
}

Limitations

  • Arrays inside JSON are indexed and flattened. For example, an array like actors will be represented as actors[0].name, actors[1].name, etc.
  • Only primitive values (string, number, boolean, null, undefined) are supported as leaf nodes in the JSON structure.

License

This project is licensed under the MIT License.