File size: 1,034 Bytes
625e9e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import json

def create_filterable_metadata():
    """
    Reads products.json to extract unique brand and category values,
    and saves them to a JSON file for later use in filtering.
    """
    try:
        with open("products.json", "r", encoding="utf-8") as f:
            products = json.load(f)
    except FileNotFoundError:
        print("Error: products.json not found.")
        return
    except json.JSONDecodeError:
        print("Error: Could not decode JSON from products.json.")
        return

    brands = sorted(list(set(p["brand"] for p in products.values() if p.get("brand"))))
    categories = sorted(list(set(p["category"] for p in products.values() if p.get("category"))))

    filterable_values = {
        "brands": brands,
        "categories": categories,
    }

    with open("filterable_metadata.json", "w", encoding="utf-8") as f:
        json.dump(filterable_values, f, indent=4)

    print("Successfully created filterable_metadata.json")

if __name__ == "__main__":
    create_filterable_metadata()