# Author: HohlenwergerMB # *Indented by tabs* # JSON Utils Import from jsonUtils import jsonReader, jsonWriter # READ data = jsonReader("broken-database.json") # FIX try: # Fix one by one temp = {} for keys in data: if "quantity" not in keys: # Add the missing key keys["quantity"] = 0 # Swap positions # Just necessary if the order of Array keys is important keys["price"] = keys.pop("price") keys["category"] = keys.pop("category") except: print("\nError: Failed to create a new key 'quantity' with value 0.") exit() # WRITE jsonWriter(data, "broken-database.json") # END print("\nEverything works! Quantities in JSON DB were repaired.")
# Author: HohlenwergerMB # *Indented by tabs* # JSON Utils Import from jsonUtils import jsonReader, jsonPrint # READ badList = jsonReader("broken-database.json") # Adapted Bubble Sort length = len(badList) - 1 unsorted = True while unsorted: unsorted = False for element in range(0, length): # Check Category Alphabetical Order if badList[element]["category"] > badList[element + 1]["category"]: # Swap badList[element], badList[element + 1] = badList[element + 1], badList[element] # Mark as not ready, need to check again unsorted = True # If in the same category... elif badList[element]["category"] == badList[element + 1]["category"]: # ...sort by id if badList[element]["id"] > badList[element + 1]["id"]: # Swap