-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson2csv.js
More file actions
111 lines (80 loc) · 2.7 KB
/
json2csv.js
File metadata and controls
111 lines (80 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var fs = require('fs');
const parse = require('csv-parse')
const csv = require('csv-parser')
const mysql = require('mysql');
const express = require('express');
var jsonexport = require('jsonexport');
const path = require('path');
const app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
//**json/csv API WORKING */
const db = mysql.createConnection({
connectionLimit: 100,
host: 'localhost',
user: 'root',
password: '',
database: 'json_parsing'
});
db.connect((err) => {
if (err) {
throw err;
}
console.log('connection established..');
});
app.listen('3000', () => {
console.log('server started on port 3000');
});
app.post('/head', function (req, res) {
fs.readFile('.//files/data.json', 'utf8', function (err, contents) {
const data = JSON.parse(contents)
var results = []
// var dot=Object.keys(data);
var nodeValues = Object.values(data);
results.push(nodeValues);
// console.log('its lan',results);
jsonexport(results, function (err, csv) {
if (err) return console.log(err);
fs.writeFile(".//files/nour.csv", csv, function (err, result) {
if (err)
console.log('error', err);
})
})
//**********get headers and store into DB */
const arrayset = [];
fs.createReadStream('.//files/nour.csv')
.pipe(csv())
.on('headers', (headers) => {
arrayset.push(headers)
let i = 0;
let text = headers;
for (i; i < text.length; i++) {
db.query('ALTER TABLE json_conversion ADD ' + text[i] + ' varchar(255)', (error, response) => {
console.log(error || response);
});
}
})
})
})
//**********get columns and store into DB */
app.post('/column', (req, res) => {
// let stream = fs.createReadStream('.\\files\\FL_insurance_sample.csv');
let jsondata = [];
fs.createReadStream('.\\files\\nour.csv')
.pipe(
parse({ delimiter: ',' }))
.on("data", function (dataRow) {
jsondata.push(dataRow);
})
.on("end", function () {
jsondata.shift();
let sql = 'ALTER TABLE json_conversion DROP id';
connection.query(sql, (error, response) => {
console.log(error || response);
});
let query = 'INSERT INTO json_conversion VALUES ?';
connection.query(query, [jsondata], (error, response) => {
console.log(error || response);
});
});
});