The Config Keeper
Another file is waiting for handoff now: a teammate needs to take over a small dashboard setup, but the configuration record is still too messy to trust. Some fields have extra spaces, some lists mix blanks with duplicates, and the same idea appears in inconsistent forms. If you pass it along like this, the next person will spend their first minutes guessing which values are supposed to count as the real ones.
Your job is to turn this JSON-style config into a clean, stable, readable record. When you are done, the next maintainer should be able to see what to keep, what is enabled, and who gets notified—without having to come back and ask you how the file is meant to be interpreted.
First, look at one smaller move: clean the items inside a config list
Do not rush into the whole config yet. Start with one smaller toy example so you can feel the move first: clean the list items, then place them back into the structure.
config = {
"theme": " twilight ",
"tools": [" charts ", "", "Charts", " alerts "]
}
clean_theme = config["theme"].strip().lower()
clean_tools = []
for tool in config["tools"]:
clean_tool = tool.strip().lower()
if clean_tool != "" and clean_tool not in clean_tools:
clean_tools.append(clean_tool)
print(clean_theme)
print(clean_tools)
This example does not process the full file. It only demonstrates one key feeling for today: when the messy strings and duplicates inside a structure get cleaned first, the whole config becomes more reliable.
Today’s deliverable: turn dashboard_config.json into a readable config record
The script already reads dashboard_config.json and stores the raw result in raw_config. You need to finish the script so the key fields, list items, and nested dictionary become tidy, then return final_config and config_summary.
Complete clean_config_list(items). Loop through each item, clean it with strip().lower(), skip blank results, and keep only the first cleaned copy.
Read workspace_name, owner, and theme from raw_config. The first two only need strip(), while theme should also become lowercase so the value stays stable.
Use clean_config_list(...) to clean features, notification_emails, and dashboard["sidebar_links"]. At the same time, clean dashboard["homepage"] into one stable lowercase string.
Build final_config from the cleaned variables and keep the nested dashboard structure. Then build config_summary so the next person can immediately see that the record has already been cleaned.
The same idea appears in different forms, blank items and duplicates are mixed in, and the nested structure is still uneven. Your job is not to expand the feature set. It is to turn this into a record that anyone can read and keep using.
Suggested SolutionExpandCollapse
import json
ASSET_FILENAME = "dashboard_config.json"
with open(ASSET_FILENAME, "r", encoding="utf-8") as file:
raw_config = json.load(file)
print("Raw config:", raw_config)
def clean_config_list(items):
cleaned_items = []
for item in items:
clean_item = item.strip().lower()
if clean_item != "" and clean_item not in cleaned_items:
cleaned_items.append(clean_item)
return cleaned_items
clean_workspace_name = raw_config["workspace_name"].strip()
clean_owner = raw_config["owner"].strip()
clean_theme = raw_config["theme"].strip().lower()
clean_features = clean_config_list(raw_config["features"])
clean_notification_emails = clean_config_list(raw_config["notification_emails"])
clean_homepage = raw_config["dashboard"]["homepage"].strip().lower()
clean_sidebar_links = clean_config_list(raw_config["dashboard"]["sidebar_links"])
final_config = {
"workspace_name": clean_workspace_name,
"owner": clean_owner,
"theme": clean_theme,
"features": clean_features,
"notification_emails": clean_notification_emails,
"dashboard": {
"homepage": clean_homepage,
"sidebar_links": clean_sidebar_links,
},
}
config_summary = {
"raw_feature_count": len(raw_config["features"]),
"kept_feature_count": len(clean_features),
"raw_email_count": len(raw_config["notification_emails"]),
"kept_email_count": len(clean_notification_emails),
"raw_sidebar_link_count": len(raw_config["dashboard"]["sidebar_links"]),
"kept_sidebar_link_count": len(clean_sidebar_links),
}
print("Final config:", final_config)
print("Config summary:", config_summary)Advanced TipsWant more? Click to expandClick to collapse
The main feeling to keep from this lesson is that a config file is not mysterious. It is another structured record that someone else needs to keep using.
If you can clean strings, organize lists, and rebuild dictionaries, you can already turn a small config that is not safe to hand off into a stable result.