-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson_file_functions.py
More file actions
60 lines (50 loc) · 1.99 KB
/
json_file_functions.py
File metadata and controls
60 lines (50 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Functions for reading and writing json files
"""
import json
import logging
from pathlib import Path
logger = logging.getLogger(__name__)
def read_json_file(file_path: Path | str) -> dict | list:
"""
Reads a json file as a dictionary or list. Includes error checking and logging.
Args:
file_path (Path | str): The file path of the json file to read.
Returns:
dict | list: The contents of the json file as a dictionary or list.
"""
try:
file_path = Path(file_path)
if not file_path.parent.exists():
file_path.parent.mkdir(parents=True, exist_ok=True)
logger.debug(f"Created folder: {json.dumps(str(file_path.parent))}")
with open(file_path, "r", encoding="utf-8") as file:
json_data = json.load(file)
logger.debug(f"Successfully read json file: {json.dumps(str(file_path))}")
return json_data
except FileNotFoundError:
logger.error(f"File not found: {json.dumps(str(file_path))}")
raise
except json.JSONDecodeError:
logger.error(f"Error decoding json file: {json.dumps(str(file_path))}")
raise
def write_json_file(data: dict | list, file_path: Path | str) -> None:
"""
Writes a dictionary or list to a json file. Includes error checking and logging.
Args:
data (dict | list): The data to write to the json file.
file_path (Path | str): The file path of the json file to write.
Returns:
None
"""
try:
file_path = Path(file_path)
if not file_path.parent.exists():
file_path.parent.mkdir(parents=True, exist_ok=True)
logger.debug(f"Created folder: {json.dumps(str(file_path.parent))}")
with open(file_path, "w", encoding="utf-8") as file:
json.dump(data, file, indent=4)
logger.info(f"Successfully wrote json file: {json.dumps(str(file_path))}")
except IOError:
logger.error(f"Error writing json file: {json.dumps(str(file_path))}")
raise