-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonexplore.py
More file actions
69 lines (55 loc) · 1.64 KB
/
Copy pathjsonexplore.py
File metadata and controls
69 lines (55 loc) · 1.64 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
from ObjBuilder import ObjBuilder
from ObjTextPrinter import ObjTextPrinter
from ObjJsonPrinter import ObjJsonPrinter
from ObjJsPrinter import ObjJsPrinter
from sys import argv
import json
use_string = """Use: python jsonexplore.py input_json_file [no_value]
input_json_file: path to the json file to explore
no_value: optional parameter saying you don't want to explore the values\n
examples:
python jsonexplore.py my_file.json
python jsonexplore.py my_file.json no_value"""
output_json_file = "obj.json"
output_js_file = "obj.js"
class InitError(Exception):
pass
try:
input_json_file = argv[1]
with_values = True
if len(argv) > 2:
if argv[2] == 'no_value':
with_values = False
else:
raise InitError("If used, the second parameter should be 'no_value'")
f = open(input_json_file, 'r')
loaded_json = json.load(f)
f.close()
builder = ObjBuilder(loaded_json)
obj = builder.get_obj()
text_printer = ObjTextPrinter()
print text_printer.render(obj, with_values)
json_printer = ObjJsonPrinter()
obj_json = json_printer.render(obj, with_values)
f = open(output_json_file, 'w')
json.dump(obj_json, f)
f.close()
#print json.dumps(obj_json)
js_printer = ObjJsPrinter(input_json_file)
obj_js = js_printer.render(obj, with_values)
f = open(output_js_file, 'w')
f.write(obj_js)
f.close()
except IndexError:
print "The parameter input_json_file is missing"
print "\n%s"% use_string
except IOError:
print "No such file or directory: '%s'"% argv[1]
print "\n%s"% use_string
except InitError as e:
print e
print "\n%s"% use_string
except ValueError as e:
print "The JSON file cannot be loaded"
print e
print "\n%s"% use_string