load_json

pyhelpers.store.load_json(path_to_file, engine=None, verbose=False, **kwargs)[source]

Load data from a JSON file.

Parameters:
  • path_to_file (str | os.PathLike) – Path where a JSON file is saved.

  • engine (str | None) – An open-source Python package for JSON serialization; valid options include None (default, for the built-in json module), 'ujson' (for UltraJSON), 'orjson' (for orjson) and 'rapidjson' (for python-rapidjson).

  • verbose (bool | int) – Whether to print relevant information in console; defaults to False.

  • kwargs – [Optional] parameters of json.load() (if engine=None), orjson.loads() (if engine='orjson'), ujson.load() (if engine='ujson') or rapidjson.load() (if engine='rapidjson').

Returns:

Data retrieved from the specified path path_to_file.

Return type:

dict

Note

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}}