-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathjson_handler.py
49 lines (35 loc) · 1.32 KB
/
json_handler.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
import json
def get_data(json_file):
with open(f'{json_file}', 'r', encoding='utf-8') as f:
jsonData = json.load(f)
verses: str = jsonData['verses']
refs: str = jsonData['references']
return verses, refs
def fix_json_structure(input_file, output_file):
with open(input_file, 'r', encoding='utf-8') as file:
data = json.load(file)
verses = data['verses']
references = data['references']
fixed_data = {'verses': []}
for i in range(len(verses)):
verse = verses[i]
reference = references[i]
fixed_data['verses'].append({'verse': verse, 'reference': reference})
with open(output_file, 'w', encoding='utf-8') as file:
json.dump(fixed_data, file, indent=4, ensure_ascii=False)
def restore_json_structure(input_file, output_file):
with open(input_file, 'r', encoding='utf-8') as file:
data = json.load(file)
verses = []
references = []
for verse_data in data['verses']:
verse = verse_data['verse']
reference = verse_data['reference']
verses.append(verse)
references.append(reference)
restored_data = {
'verses': verses,
'references': references
}
with open(output_file, 'w', encoding='utf-8') as file:
json.dump(restored_data, file, indent=4, ensure_ascii=False)