https://github.com/rogue-elephant/json-conversion-tool
Provides functionality for converting JSON objects into a relational structure and then outputting to various formats (csv, markdown, html etc.)
https://github.com/rogue-elephant/json-conversion-tool
json json-conversion json-to-csv json-to-html json-to-markdown
Last synced: 2 months ago
JSON representation
Provides functionality for converting JSON objects into a relational structure and then outputting to various formats (csv, markdown, html etc.)
- Host: GitHub
- URL: https://github.com/rogue-elephant/json-conversion-tool
- Owner: rogue-elephant
- License: mit
- Created: 2019-12-08T15:32:25.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-05T02:34:25.000Z (over 2 years ago)
- Last Synced: 2025-01-20T20:53:27.594Z (4 months ago)
- Topics: json, json-conversion, json-to-csv, json-to-html, json-to-markdown
- Language: TypeScript
- Size: 912 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JSON-CONVERSION-TOOL
#### NPM
[](https://www.npmjs.com/package/json-conversion-tool)
[](https://libraries.io/npm/json-conversion-tool)
[](https://snyk.io/vuln/search?q=json-conversion-tool&type=npm)
 #### Github



## Description
Provides functions for converting JSON objects to and from CSVs.## Try me out
[](https://gitpod.io#https://github.com/rogue-elephant/json-conversion-tool)# Usage
## Example
``` typescript
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 + `${table.title}
` + '' + table.columnNames.map(x => `${x}`).join('') + '',
rowLevelCallback: (rowCol: IRowValue) => `${rowCol.value}`,
rowStartOutput: '',
rowEndOutput: '',
tableEndOutput: '',
tableSpacing: '
',
});
```### 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,,1Awards
year,title
2016,Best at Everythingwarnings
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](#Awards)|
2|Jane|Doe|Mrs|Sales|Sales Executive||
3|John|Doe|Mr|R&D|Data Scientist||[1](#warnings)# Awards
|*year*|*title*
|---|---
2016|Best at Everything# warnings
|*year*|*reason*
|---|---
2016|Farted in the coffee machine
----------------------------------------------------------------------------------------------### HTML (custom) Output
``` htmlConverted 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
```
## Dot notation
You can also specify specifc fields to get based on dot notation, such as:
``` typescript
new OutputGenerator({ new Converter().convertJson(apiJson,{whiteList: ['prefs.backgroundImageScaled.url'])});
```Or
``` typescript
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