-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodularJSONPARSER.PY
More file actions
164 lines (118 loc) · 3.49 KB
/
Copy pathmodularJSONPARSER.PY
File metadata and controls
164 lines (118 loc) · 3.49 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import json
from pprint import pprint
from pymongo import MongoClient
import sys
import os
s= sys.argv[1]
base=os.path.basename(s)
filename = os.path.splitext(base)[0]
f = open(s,"r")
lines = f.readlines()
num_lines = sum(1 for line in open(s))
print num_lines
last = lines[-1]
client = MongoClient('localhost:27017')
db1 = client.cis550
listID = 0
mapID = 0
listobj = 0
def printKeyVals(key,val):
global id
global listID
global mapID
global listobj
if isinstance(val, list):
id = id+1
listID = listID+1
f.write( '"'+str(id)+'"' + ' : { ' )
f.write( '"'+key+'"' + ' : [' )
if(len(val)!=0 and isinstance(val[0], list) == False and isinstance(val[0], dict) == False):
listobj = 1
result = db1.node.insert_one({"node_id": str(id), "key":str(key), "value": val, "root":str(root)})
else:
listobj=0
result = db1.node.insert_one({"node_id": str(id), "key":str(key), "value":"list"+str(listID), "root":str(root)})
if(len(val)==0):
f.write("]}")
return
printKeyVals(key,val[0])
val = val[1:]
for each in val:
f.write(" , ")
printKeyVals(key,each)
f.write(" ] } ")
listobj=0
elif isinstance (val, dict):
id = id+1
f.write('"'+str(id)+'"'+' : { ')
f.write('"'+key+'"'+ ' : {')
mapID = mapID+1
result = db1.node.insert_one({"node_id": str(id), "key":str(key), "value":"map"+str(mapID), "root":str(root)})
if(len(val)==0):
f.write("}}")
return
i = len(val)
for k,v in val.iteritems():
#id generation
i = i-1
if ( (isinstance(v, list) == False) and (isinstance(v,dict) == False) ) :
id=id+1
f.write('"'+str(id)+'"' + ' : { ')
f.write('"' + k + '"'+ " :")
printKeyVals(k,v)
if(isinstance(v,dict) == False):
f.write(" } ")
if(i!=0):
f.write(" , ")
f.write("}")
f.write(" } ")
else:
if(isinstance(val,basestring)==False) :
f.write('"'+str(val)+'"')
result = db1.node.insert_one({"node_id": str(id), "key":str(key), "value":str(val), "root":str(root)})
else:
dummyval = val.encode('ascii','ignore')
dummyval = dummyval.replace('\n',' ')
dummyval = dummyval.replace('"','')
f.write('"'+ dummyval.lower()+'"')
if(listobj!=1):
result = db1.node.insert_one({"node_id": str(id), "key":str(key), "value": dummyval, "root":str(root)})
i = int(sys.argv[2])
id = long(i)
f= open("new.json","w")
f.write("")
f.close()
f = open("new.json","a")
id = id+1
root = id
f.write(' { '+ '"'+str(id)+'"' +' : '+' { ' '"'+filename+'"'+' : ')
f.write('\n { ')
result = db1.node.insert_one({"node_id": str(root), "key":str(filename), "value":"file", "root":str(root)})
for data_line in lines:
data_line.replace('\n', ' ')
data_file = data_line.encode('ascii','ignore')
data = json.loads(data_file)
i = len(data.keys());
for key in data.keys():
i = i-1
if ( (isinstance(data[key], list) == False) and (isinstance(data[key],dict) == False) ) :
id = id+1
f.write('"'+str(id)+'"' +':{')
f.write('"' + key + '"'+" :")
printKeyVals(key,data[key])
f.write('}')
else:
printKeyVals(key,data[key]);
if(i!=0):
f.write(' , ')
if(data_line != lines[-1]):
f.write(','+'\n ')
else :
f.write('} \n')
f.write(' } }\n')
f.close()
db = client.test
with open('./new.json') as json_read:
upload = json.load(json_read)
result = db.test.insert_one(upload)
print id