import json


def create_dspace_metadata_from_dict(value_dict, metadata_mapping_path):
    """Create DSpace JSON metadata from metadata dict and a JSON metadata mapping file."""
    with open(metadata_mapping_path, "r") as metadata_mapping:
        metadata_mapping = json.load(metadata_mapping)
        metadata = []
        for key in [k for k in metadata_mapping if k in value_dict.keys()]:
            if isinstance(value_dict[key], list):
                for list_item in value_dict[key]:
                    metadata.append({"key": metadata_mapping[key], "value": list_item})
            else:
                metadata.append(
                    {"key": metadata_mapping[key], "value": value_dict[key]}
                )
        return {"metadata": metadata}