fayezor of Gamalon
10/8/2019 - 8:36 PM

Convert response csv to response json

import csv
import json

def response_csv_to_json(response_csv_filepath,
                         noClassification_global = 'Please clarify.'): # global noClassification response is required
    
    response_dict["_globals_"] = {"intro": list(), 
                                  "noClassification": noClassification_global}

    with open(response_csv_filepath, 'r') as f:
        response_csv = csv.reader(f)
        next(response_csv)
        for row in response_csv:
            path, responseText, buttons, filterLayers = row
            response_dict[path] = {'responseText': responseText,
                                   'buttons': buttons or list(),
                                   'filterLayers': filterLayers or list()}    
    return response_dict

response_dict = response_csv_to_json('responses.csv')

with open('responses.json', 'w') as f:
    json.dump(response_dict, f)