################################################################################
# - Read a JSON file into a list of dictionaries
# - Write a list of dictionaries into a JSON file
################################################################################
import json

### Read a JSON file into a list of dictionaries ###############################
def readJSON(filename):
    list = []

    try:
        with open(filename) as jsonfile:            #open file for read 
            list = json.load(jsonfile)              #read JSON and convert into a list of dictionaries
    except Exception as e:
        print(F"Error reading input file - {e}")

    return(list)                                    #a list of dictionaries      


### Write a list of dictionaries into a JSON file ##############################
def writeJSON(list, filename):

    if not list: return(False)                      #if empty list, return 
 
    try:
        with open(filename,"w") as jsonfile:        #open the file for write
            json.dump(list, jsonfile, indent=4)     #convert a list of dictionaries into JSON and write
    except Exception as e:
        print(F"Error writing to output file - {e}")
        return(False)
 
    return(True)                      


### main code (test only) ######################################################

# input_file  = '/users/samsu/desktop/students.json'
# output_file = '/users/samsu/desktop/students2.json'

# list  = readJSON(input_file)
# is_ok = writeJSON(list, output_file)