-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapt_json.py
35 lines (25 loc) · 1.03 KB
/
adapt_json.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
import json
import json
def transform_json(original_file, transformed_file):
with open(original_file, 'r') as f:
original_data = json.load(f)
transformed_data = {'data': []}
for item in original_data['data']:
for p in item['paragraphs']:
for qa in p['qas']:
for ans in qa['answers']:
answer = ans
question = qa['question']
document = p['context']
id = qa['id']
# Create a new element in the transformed data structure
transformed_item = {'id': id, 'context': document, 'question': question, 'answer': answer}
# Append to the 'data' list
transformed_data['data'].append(transformed_item)
break
# Save the transformed data as JSON
with open(transformed_file, 'w') as f:
json.dump(transformed_data, f, indent=4)
# Example usage
transform_json('/data/COVID-QA.json',
'data/data.json')