File size: 714 Bytes
074b318
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import json
from typing import List, Dict


def read_json_to_dict_array(file_path: str) -> List[Dict]:
    """
    Read a JSON file and convert its content to an array of dictionaries.

    :param file_path: Path to JSON file.
    :return: Array or dicts.
    """
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            data = json.load(f)
            if isinstance(data, list):
                return data
            else:
                raise ValueError(f"Wrong data type. Expected list. Got: {str(type(data))}")
    except FileNotFoundError:
        print(f"File '{file_path}' not found")
    except json.JSONDecodeError as e:
        print(f"Failed to decode JSON: {e}")
    return []