load_json¶
- pyhelpers.store.load_json(path_to_file, engine=None, verbose=False, prt_kwargs=None, raise_error=False, **kwargs)[source]¶
Loads data from a JSON file.
- Parameters:
path_to_file (str | os.PathLike) – Path where the JSON file is saved.
engine (str | None) – An open-source Python package for JSON serialisation; valid options include
None
(default, for the built-in json module),'ujson'
(for UltraJSON),'orjson'
(for orjson) and'rapidjson'
(for python-rapidjson); defaults toNone
.verbose (bool | int) – Whether to print relevant information to the console; defaults to
False
.prt_kwargs (dict | None) – [Optional] Additional parameters for the function
pyhelpers.store._check_loading_path()
; defaults toNone
.raise_error (bool) – Whether to raise the provided exception; if
raise_error=False
(default), the error will be suppressed.kwargs – [Optional] Additional parameters for json.load() (if
engine=None
), orjson.loads() (ifengine='orjson'
), ujson.load() (ifengine='ujson'
) or rapidjson.load() (ifengine='rapidjson'
).
- Returns:
Data retrieved from the specified path
path_to_file
.- Return type:
dict
Note
Example data can be referred to in the function
save_json()
.
Examples:
>>> from pyhelpers.store import load_json >>> from pyhelpers.dirs import cd >>> json_path = cd("tests", "data", "dat.json") >>> json_dat = load_json(json_path, verbose=True) Loading ".\tests\data\dat.json" ... Done. >>> json_dat {'London': {'Longitude': -0.1276474, 'Latitude': 51.5073219}, 'Birmingham': {'Longitude': -1.9026911, 'Latitude': 52.4796992}, 'Manchester': {'Longitude': -2.2451148, 'Latitude': 53.4794892}, 'Leeds': {'Longitude': -1.5437941, 'Latitude': 53.7974185}}