F1 circuit layouts with year-by-year SVGs — manually traced track variations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.5KB

  1. import json
  2. def update_keys_with_slugs(data):
  3. if not isinstance(data, dict):
  4. return data
  5. new_dict = {}
  6. for key, value in data.items():
  7. if isinstance(value, dict):
  8. # Store original key as name if it's not already present
  9. if 'name' not in value:
  10. value['name'] = key
  11. # Get the slug and remove it from the value dict
  12. slug = value.pop('slug', key)
  13. # Recursively process nested dictionaries
  14. processed_value = update_keys_with_slugs(value)
  15. # Use the slug as the new key
  16. new_dict[slug] = processed_value
  17. else:
  18. new_dict[key] = value
  19. return new_dict
  20. def main():
  21. try:
  22. # Read the JSON file with UTF-8 encoding
  23. with open('./circuits.json', 'r', encoding='utf-8') as file:
  24. data = json.load(file)
  25. # Process the data
  26. updated_data = update_keys_with_slugs(data)
  27. # Write the updated data back to the file with UTF-8 encoding and ensure_ascii=False
  28. with open('./circuits-new.json', 'w', encoding='utf-8') as file:
  29. json.dump(updated_data, file, indent=2, ensure_ascii=False)
  30. print("Successfully updated circuit keys with slugs")
  31. except FileNotFoundError:
  32. print("Error: circuits.json file not found")
  33. except json.JSONDecodeError:
  34. print("Error: Invalid JSON format in circuits.json")
  35. except Exception as e:
  36. print(f"An error occurred: {str(e)}")
  37. if __name__ == "__main__":
  38. main()