-
Notifications
You must be signed in to change notification settings - Fork 7
/
importList.js
57 lines (30 loc) · 1.05 KB
/
importList.js
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
/*
made by qwazwsx/thisisatesttoseehowl
https://github.com/qwazwsx
generates JSON formatted data from a newline seperated text file
import with:
mongoimport --db <DB_NAME> --collection <COLLECTION_NAME> --drop --jsonArray --file <FILE_LOCATION>
mongoimport --db c4-cams --collection camera_list --drop --jsonArray --file list.json
^^^^^^
WARNING: running the command above WILL DROP THE DATABASE before importing
*/
var fs = require('fs');
//setup array
var txt = []
//read from newline seperated file, list.txt
fs.readFile('list.txt','utf8', function(err,d){
//split by newline
var data = d.split('\n')
//loop through all lines
for (var i = 0; i < data.length; i++){
//add line to array
txt.push({url: data[i]})
}
//write array to file, list.json
fs.writeFile("list.json", JSON.stringify(txt), function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
});