Skip to content

Commit d6d3a9c

Browse files
committed
adds option to return JSON data as string or dict
1 parent 53360f6 commit d6d3a9c

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

uk_covid19/api_interface.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ def _get(self, format_as: str) -> Iterator[Response]:
234234

235235
api_params["page"] += 1
236236

237-
def get_json(self, save_as: Union[str, None] = None) -> dict:
237+
def get_json(self, save_as: Union[str, None] = None, as_string: bool = False) -> Union[dict, str]:
238238
"""
239239
Provides full data (all pages) in JSON.
240240
@@ -247,9 +247,13 @@ def get_json(self, save_as: Union[str, None] = None) -> dict:
247247
The value must be a path to a file with the correct
248248
extension -- i.e. ``.json`` for JSON).
249249
250+
as_string: bool
251+
If ``False`` (default), returns the data as a dictionary.
252+
Otherwise, returns the data as a JSON string.
253+
250254
Returns
251255
-------
252-
Dict
256+
Union[Dict, str]
253257
254258
Examples
255259
--------
@@ -287,6 +291,9 @@ def get_json(self, save_as: Union[str, None] = None) -> dict:
287291
data = dumps(resp, separators=(",", ":"))
288292
save_data(data, save_as, "json")
289293

294+
if as_string:
295+
return dumps(resp, separators=(",", ":"))
296+
290297
return resp
291298

292299
def get_xml(self, save_as=None) -> XMLElement:

0 commit comments

Comments
 (0)