AI_Agro_Support/pipeline/jsonutils.py
Arsham Mirehvandi 099d1e0af9 Initial commit
2026-08-22 08:47:56 +02:00

30 lines
822 B
Python

"""Helpers for the JSON-encoded list columns stored as text in SQL Server."""
from __future__ import annotations
import json
from typing import Any
def parse_json_list(raw: Any) -> list[str]:
"""Decode a JSON array column into a list of strings; unparseable input yields []."""
if raw is None:
return []
if isinstance(raw, list):
return [str(item) for item in raw]
text = str(raw).strip()
if not text:
return []
try:
parsed = json.loads(text)
if isinstance(parsed, list):
return [str(item) for item in parsed]
except json.JSONDecodeError:
pass
return []
def normalize_name(product_name: str) -> str:
"""Key used for case- and whitespace-insensitive product name comparison."""
return product_name.strip().casefold()