-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanJson.py
103 lines (93 loc) · 3.79 KB
/
cleanJson.py
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
# -*- coding: utf-8 -*-
"""
Created on 10 March 2015
@author: mattcook
"""
import simplejson, os, re, sys, getopt
# this is path to file
# uncomment to direct to path of file
#os.chdir(r'/Users/username/path')
# this is name of file
fileName = ""
# this is name of new clean file to be made
newFileName = ""
invalidLinesFile = "badlines.txt"
usage = "usage: python cleanJson.py -i inputFile.json -o outputFile.json [-f | -t]"
def main(argv):
fillerLine = ""
try:
opts, args = getopt.getopt(argv,"hi:o:tf",["ifile=","ofile=","--help", "--freqOffset", "--timestamps"])
except getopt.GetoptError:
print usage
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
print usage
sys.exit()
elif opt in ("-i", "--ifile"):
fileName = arg
elif opt in ("-o", "--ofile"):
newFileName = arg
elif opt in ("-f", "--freqOffset"):
fillerLine = "{\"date\": -1, \"time\": 0.0, \"freqOffset\": 0.0}]"
elif opt in ("-t", "--timestamps"):
fillerLine = "{\"date\": -1, \"time\": 0.0, \"originTS\": 0.0, \"receiveTS\": 0.0, \"transmitTS\": 0.0, \"destTS\": 0.0}]"
if fillerLine == "" or fileName == "" or newFileName == "":
print usage
sys.exit(0)
fillerEndLine = fillerLine + "}]\n"
fillerMidLine = fillerLine + "},\n"
def checkForError(f):
try:
simplejson.loads(f)
return True
except Exception as e:
return False
def makeJsonLines(line):
return "[" + line + "\n" + fillerLine
# loop through checking for errors
lineCounter = 0
firstNode = True
with open(fileName, 'r') as json_data:
with open(newFileName, 'w') as newJsonData:
with (open(invalidLinesFile, 'w')) as badlines:
for line in json_data:
lineCounter += 1
# check for the final line in JSON file
if ("}]}]" in line):
if (checkForError(makeJsonLines(line.replace("}]}]", "},")))):
newJsonData.write(line)
else:
print "Error in line", lineCounter
print line
newJsonData.write(fillerEndLine)
# check for the end of one node
elif ("}]}," in line):
line = line.replace("}]}", "}")
if (checkForError(makeJsonLines(line))):
newJsonData.write(line)
else:
print "Error in line", lineCounter
print line
newJsonData.write(fillerMidLine)
# check for first line in node data
elif ("\"node\"" in line):
# make sure we don't put an invalid line for the very first one
if (firstNode):
firstNode = False
else:
newJsonData.write(fillerMidLine)
line = re.sub(r'"entries":\[.+', r'"entries":[', line)
newJsonData.write(line)
# check if this line is valid
elif (checkForError(makeJsonLines(line))):
newJsonData.write(line)
# if not, throw away this line
else:
#print "Error in line", lineCounter
#print line
badlines.write(line)
badlines.write("\n")
print "Done Checking!"
if __name__ =='__main__':
main(sys.argv[1:])