Untitled
unknown
plain_text
3 years ago
1.7 kB
12
Indexable
import os
import json
json_folder = '/Users/kyle.kahraman/seadrive_root/kyle.kah/Shared with me/shared_Kyle_Prav/MEG_subjects/kyle test/formatted json directory'
output_file = "/Users/kyle.kahraman/seadrive_root/kyle.kah/Shared with me/shared_Kyle_Prav/MEG_subjects/kyle test/working.json"
for filename in os.listdir(json_folder):
if filename.endswith(".json"):
with open(os.path.join(json_folder, filename), 'r') as f:
data = json.load(f)
# get the data into individual lines
lines = data[''].split('\n')
# remove + and unnecessary | symbols from lines, clean them up
lines = [line.strip('|') for line in lines if line.find('+') == -1]
# start extracting specific information that we want, e.g. participant id
participant_id = [line.split(':')[1] for line in lines if line.find('Participant ID') != -1]
# further cleaning up, now we have
participant_id = participant_id[0].strip(' ').strip('|').strip(' ')
print(participant_id)
# load original json file / template
with open('/Users/kyle.kahraman/seadrive_root/kyle.kah/Shared with me/shared_Kyle_Prav/MEG_subjects/meg_subjects_demographic_data.json', 'r') as f:
myjson = json.load(f)
# in that json file set subject_id / subject name field to the participant id that we have extracted e.g. myjson['subject_id'] = participant_id
myjson['subject_id'] = participant_id
# similarly extract other fields and put them into json template
# save the json template back with all fields updated
with open('updated_template.json', 'w') as f:
json.dump(myjson, f)
Editor is loading...