Giter VIP home page Giter VIP logo

json-conversion-tool's Introduction

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

json-conversion-tool's People

Contributors

dependabot[bot] avatar rogue-elephant avatar

Stargazers

 avatar

Watchers

 avatar

json-conversion-tool's Issues

Transformation strategies - standardize similar values

Add ability to specify transformations that can be performed during the conversion - standardizing similar values, only convert specific properties (whitelist and blacklist)

Standardize similar values (e.g. Hr, hr, HR -> HR)

Change conversion class to 'Converter'

Change the conversion class to a converter class that can take different strategies as a parameter - the first of these will be json to csv, the second will be csv to json.

Vs code extension

Create vs code extension that uses the json conversion tool - cmd palette > convert current file > choose output type

Lookups

Ability to add lookups from within a complex JSON structure - for example if there are custom field definitions or enums specified in one place in the JSON and then the key for these referenced in later properties.

Relational structure

When a one-to-many relationship is identified a reference to this should be stored in the conversion object.

JSON schema files

Ability to configure the conversion for schema files to output properties as markdown or markdown tables and to look up refs in other areas or files (this should work via the same mechanism as the lookups in task rogue-elephant/json-csv-tool#11 )

Markdown output

Ability to output to markdown instead of csv - can use this then for creating wiki documentation for JSON objects - particularly json schemas.

Change name to json-conversion-tool

The name should be json-conversion-tool and it's main aim should be storing JSON data into a relational format that then can be output at csv or markdown or whatever

Start from property

Need the ability to set a start from property probably needs to be by path to get to nested items.
This means that you can have a complex JSON but only iterate rows from some nested property within it rathe than the results being flattened to item_0, item_1 etc.

Complex JSON objects

Ability to convert more complex JSON with nested object properties and array properties.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.