in src/pixparse/utils/json_utils.py [0:0]
def normalize_dict(self, data: Union[Dict, List, Any]):
"""
Sort by value, while iterate over element if data is list
"""
if not data:
return {}
if isinstance(data, dict):
new_data = dict()
for key in sorted(data.keys(), key=lambda k: (len(k), k)):
value = self.normalize_dict(data[key])
if value:
if not isinstance(value, list):
value = [value]
new_data[key] = value
elif isinstance(data, list):
if all(isinstance(item, dict) for item in data):
new_data = []
for item in data:
item = self.normalize_dict(item)
if item:
new_data.append(item)
else:
new_data = [str(item).strip() for item in data if type(item) in {str, int, float} and str(item).strip()]
else:
new_data = [str(data).strip()]
return new_data