-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_converter.py
86 lines (73 loc) · 2.49 KB
/
json_converter.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
import json # IMPORTS JSON MODULE
dict1 = {} # DICTIONARY TO HOLD THE QUESTIONS DATA
# METHOD TO FORMAT THE QUESTIONS INTO A DICTIONARY
def format_to_dictionary(question_number, # QUESTION NUMBER
author_GUID, # AUTHOR GUID
author_name, # AUTHOR'S NAME
reviewer_GUID, # REVIEWER'S GUID
reviewer_name, # REVIEWER NAME
question, # QUESTION
options, # ARRAY OF OPTIONS
hint, # HINT TO THE QUESTION
correct_answer, # CORRECT ANSWER
option_count, # OPTION COUNT
topic): # QUESTION TOPIC
'''
This function formatts the data into a dictionary
List Of Parameters
Question_number - String
author_GUID - String
author_name - String
reviewer_GUID - String
reviewer_name - String
question - LaTex Formatted String
options - List
hint - String
correct_answer - Integer
option_count - Integer
topic - String
'''
dict1["Author of below question"] = author_name
dict1["topic"] = topic
data = {
"content": question,
"answer": [],
"hint": hint,
"credits": {
"author": {
"id": author_GUID,
"name": author_name
},
"reviewer": {
"id": reviewer_GUID,
"name": reviewer_name
}
}
}
for i in range(option_count):
answer_dict = {}
answer_dict["id"] = i+1
answer_dict["value"] = (options[i])
if(options[i] == correct_answer):
answer_dict["is_key"] = True
else:
answer_dict["is_key"] = False
data["answer"].append(answer_dict)
dict1[question_number] = data
def create_json(filename):
out_file = open(filename, "w") # CREATES OR JSON FILE
json.dump(dict1, out_file, indent=6) # DUMPS DICTIONARY INTO THE JSON FILE
out_file.close() # CLASES THE JSON FILE
def print_json(file_name):
# Opening JSON file
f = open(file_name)
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
for i in data:
print(i)
print(data[i])
# Closing file
f.close()