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-conversion-tool

v2.0.3

Published

Provides functionality for converting JSON objects into a relational structure and then outputting to various formats (csv, markdown, html etc.)

Downloads

9

Readme

JSON-CONVERSION-TOOL

NPM

json-conversion-tool npm

libraries io snyk

Github

Description

Provides functions for converting JSON objects to and from CSVs.

Try me out

Open in Gitpod

Usage

Example

const deeperPersonJson = [
    {
        id: 1,
        personalInfo: {
            firstName: 'John',
            lastName: 'Smith',
            title: 'Mr'
        },
        jobInfo: {
            department: 'HR',
            title: 'HR Assistant',
        },
        Awards: [
            {
                year: 2016,
                title: 'Best at Everything'
            }
        ]
    },
    {
        id: 2,
        personalInfo: {
            firstName: 'Jane',
            lastName: 'Doe',
            title: 'Mrs'
        },
        jobInfo: {
            department: 'Sales',
            title: 'Sales Executive',
        }
    },
    {
        id: 3,
        personalInfo: {
            firstName: 'John',
            lastName: 'Doe',
            title: 'Mr'
        },
        jobInfo: {
            department: 'R&D',
            title: 'Data Scientist',
        },
        warnings: [
            {
                year: 2016,
                reason: 'Farted in the coffee machine'
            }
        ]
    },
];

const relationalJson: RelationalJson = new Converter().convertJson(DeeperPersonJson, {});
const outputGenerator = new OutputGenerator(relationalJson);
const csv = outputGenerator.generateCsv();
const markdown = outputGenerator.generateMarkdown();
const html = outputGenerator.generateOutput({
  columnSeperator: '',
  tableLevelCallback: (output: string, table: RelationalJson) =>
    output + `<h1>${table.title}</h1>` + '<table><tr>' + table.columnNames.map(x => `<th>${x}</th>`).join('') + '</tr>',
  rowLevelCallback: (rowCol: IRowValue) => `<td>${rowCol.value}</td>`,
  rowStartOutput: '<tr>',
  rowEndOutput: '</tr>',
  tableEndOutput: '</table>',
  tableSpacing: '<br>',
});

CSV Output

id,personalInfo.firstName,personalInfo.lastName,personalInfo.title,jobInfo.department,jobInfo.title,Awards,warnings
1,John,Smith,Mr,HR,HR Assistant,1,
2,Jane,Doe,Mrs,Sales,Sales Executive,,
3,John,Doe,Mr,R&D,Data Scientist,,1

Awards
year,title
2016,Best at Everything

warnings
year,reason
2016,Farted in the coffee machine

Markdown Output


Converted JSON

|id|personalInfo.firstName|personalInfo.lastName|personalInfo.title|jobInfo.department|jobInfo.title|Awards|warnings |---|---|---|---|---|---|---|--- 1|John|Smith|Mr|HR|HR Assistant|1| 2|Jane|Doe|Mrs|Sales|Sales Executive|| 3|John|Doe|Mr|R&D|Data Scientist||1

Awards

|year|title |---|--- 2016|Best at Everything

warnings

|year|reason |---|--- 2016|Farted in the coffee machine

HTML (custom) Output

<h1>Converted JSON</h1>
<table>
    <tr>
        <th>id</th>
        <th>personalInfo.firstName</th>
        <th>personalInfo.lastName</th>
        <th>personalInfo.title</th>
        <th>jobInfo.department</th>
        <th>jobInfo.title</th>
        <th>Awards</th>
        <th>warnings</th>
    </tr>
    <tr>
        <td>1</td>
        <td>John</td>
        <td>Smith</td>
        <td>Mr</td>
        <td>HR</td>
        <td>HR Assistant</td>
        <td>1</td>
    </tr>
    <tr>
        <td>2</td>
        <td>Jane</td>
        <td>Doe</td>
        <td>Mrs</td>
        <td>Sales</td>
        <td>Sales Executive</td>
    </tr>
    <tr>
        <td>3</td>
        <td>John</td>
        <td>Doe</td>
        <td>Mr</td>
        <td>R&D</td>
        <td>Data Scientist</td>
        <td>1</td>
    </tr>
</table>
<br>
<h1>Awards</h1>
<table>
    <tr>
        <th>year</th>
        <th>title</th>
    </tr>
    <tr>
        <td>2016</td>
        <td>Best at Everything</td>
    </tr>
</table>
<br>
<h1>warnings</h1>
<table>
    <tr>
        <th>year</th>
        <th>reason</th>
    </tr>
    <tr>
        <td>2016</td>
        <td>Farted in the coffee machine</td>
    </tr>
</table>

Dot notation

You can also specify specifc fields to get based on dot notation, such as:

new OutputGenerator({ new Converter().convertJson(apiJson,{whiteList: ['prefs.backgroundImageScaled.url'])});

Or

new OutputGenerator({ new Converter().convertJson(apiJson,{whiteList: ['name', 'cards.*'])});

Contributing

  • Clone the repo and run npm install.
  • Create a new feature branch: git checkout -b feature/your-feature-branch-name.
  • Write a test in the __tests__ folder for your feature.
  • Write the code to get the test passing, running npm run test.
  • Push your branch up and submit a pull request.

Note: I have configured a launch.json for vscode that should allow for playing around with anything in index.ts and hitting f5 to debug.

Publishing

  • npm version patch
  • npm publish