-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsvTojson.js
More file actions
127 lines (123 loc) · 6.2 KB
/
Copy pathcsvTojson.js
File metadata and controls
127 lines (123 loc) · 6.2 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* Created by GH316885 on 2/22/2016.
*/
var fs = require("fs");
var lookupTable=["Oilseeds","Agriculture","Crops","Foodgrains","Kharif","Rabi","Commercial Crops","Rice","Yield","Andhra Pradesh","Karnataka","Kerala","Tamil Nadu"];
var superTextoilseeds="Agricultural Production Oilseeds",superTextfoodGrains="Agricultural Production Foodgrains";
function naToZero(value){
return value=="NA"?0:value;
}
function jsonFileGenerator(jsonObject,filename)//Json Generator function
{
var data = JSON.stringify(jsonObject);
fs.writeFile('../../jsonFile/'+filename+'.json', data, function (err) {
if (err) {
console.log('There has been an error saving your json file:');
console.log(err.message);
return;
}
console.log(filename+'.json:'+'File generated successfully');
});
}
function csvToJson(){
// Asynchronous read
fs.readFile('../../Data/Production_Agriculture.csv', function (err, data) {
if (err) {
return console.error(err);
}
var flag=0;
var header=[];
var oilSeedsJsonObject=[],foodGrainsJsonObject=[],commercialCropsJsonObj=[],riceSouthernProductionJsonObj=[];
var oilSeedsObj={},foodGrainsObj={},commercialCropsObj={},riceSouthernProductionObj={};
var lines=data.toString().split('\n');
header=lines.splice(0,1);
header=header.toString().split(',');
header.splice(0,3);
//Year Scanning
for (indVal in header) {
var year=header[indVal].trim().split('-');
commercialCropsObj={x:"Yr"+"_"+year[1],y:0};
commercialCropsJsonObj.push(commercialCropsObj);
riceSouthernProductionObj={x:"Yr"+"_"+year[1],"Andhra Pradesh":0,"Karnataka":0,"Kerala":0,"Tamil Nadu":0};
riceSouthernProductionJsonObj.push(riceSouthernProductionObj);
}
//Data Scanning
for (line in lines) {
if (lines[line].length!=0) {
var value = lines[line].match(/(".*?"|[^",]+)(?=\s*,|\s*$)/g);//For CSV quotation Handling
//OilSeeds Filter
if (value[0].indexOf(lookupTable[0]) >= 0) {
if (value[0].indexOf(lookupTable[1]) == -1 && value[0].indexOf(lookupTable[2]) == -1) {
if (value[0].split(' ').length > 4) {
oilSeedsObj = {x: value[0].replace(superTextoilseeds, ""), y: parseFloat(value[23])};
oilSeedsJsonObject.push(oilSeedsObj);
}
}
}
//FoodGrains Filter
if (value[0].indexOf(lookupTable[3]) != -1) {
if (value[0].indexOf(lookupTable[4]) != -1 || value[0].indexOf(lookupTable[5]) != -1) {
if (value[0].indexOf(lookupTable[1]) == -1 && value[0].split(' ').length >= 5) {
if (value[0].lastIndexOf(lookupTable[3]) == value[0].indexOf(lookupTable[3])) {
foodGrainsObj = {
x: value[0].replace(superTextfoodGrains, ""),
y: parseFloat(value[23])
};
foodGrainsJsonObject.push(foodGrainsObj);
}
}
}
}
//CommercialCrops Filter
if (value[0].indexOf(lookupTable[6]) != -1 && value[0].split(' ').length == 5) {
for (var i = 3; i < 25; i++) {
value[i] = naToZero(value[i].trim());
commercialCropsJsonObj[i - 3].y +=parseFloat(value[i]);
commercialCropsJsonObj[i-3].y=Math.ceil(commercialCropsJsonObj[i-3].y*1000)/1000;
}
}
//Southern India Rice Production Filter
if (value[0].indexOf(lookupTable[7]) != -1 && value[0].indexOf(lookupTable[8]) != -1) {
if (value[0].indexOf(lookupTable[9]) != -1 || value[0].indexOf(lookupTable[10]) != -1 || value[0].indexOf(lookupTable[11]) != -1 || value[0].indexOf(lookupTable[12]) != -1) {
for (var i = 3; i < 25; i++) {
value[i] = naToZero(value[i].trim());
if (value[0].indexOf(lookupTable[9]) != -1) {
riceSouthernProductionJsonObj[i - 3]["Andhra Pradesh"] += parseFloat(value[i]);
}
if (value[0].indexOf(lookupTable[10]) != -1) {
riceSouthernProductionJsonObj[i - 3]["Karnataka"] += parseFloat(value[i]);
}
if (value[0].indexOf(lookupTable[11]) != -1) {
riceSouthernProductionJsonObj[i - 3]["Kerala"] += parseFloat(value[i]);
}
if (value[0].indexOf(lookupTable[12]) != -1) {
riceSouthernProductionJsonObj[i - 3]["Tamil Nadu"] += parseFloat(value[i]);
}
}
}
}
}
else{
break;
}
}
//Sorting the oilSeeds Production in descending order
oilSeedsJsonObject.sort(function (a, b) {
return b.y - a.y;
});
//sorting the foodGrains Production in descending order
foodGrainsJsonObject.sort(function (a, b) {
return b.y - a.y
});
//calling the jsonfile Generator function for OilseedsJsonObject
jsonFileGenerator(oilSeedsJsonObject, "oilSeeds");
//calling the jsonfile Generator function for foodGrains
jsonFileGenerator(foodGrainsJsonObject, "foodGrains");
//calling the jsonfile Generator function for commercialCropsJsonObjects
jsonFileGenerator(commercialCropsJsonObj, "commercialCrops");
//calling the jsonfile Generator function for riceSouthernProductionJsonObjects
jsonFileGenerator(riceSouthernProductionJsonObj, "riceSouthernProduction");
});
}
//Calling the csvToJson function for generating the required json file.
csvToJson();