from pathlib import Path
import gzip
import json


def read_json(filename) -> dict:
    """Read json files and return a dict. (.json, .json.gz)"""
    if isinstance(filename, Path):
        filename = str(filename)
    if filename.endswith(".json.gz"):
        with gzip.open(filename, "r") as file:
            json_bytes = file.read()
            json_str = json_bytes.decode("utf-8")
            return json.loads(json_str)
    elif filename.endswith(".json"):
        with open(filename, "r") as file:
            return json.load(file)
    else:
        raise IOError("File format must be .gz or .json.gz")