Skip to content

Commit

Permalink
json_input: If json_dict is not a dict, return a useful error/warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjwebb committed Jul 4, 2024
1 parent 9261d6f commit 72fe6ab
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Ignore null characters in the input CSV file when reading configuration from the header rows
https://github.com/OpenDataServices/flatten-tool/pull/446

### Changed

- If `json_dict` is not a dict, return a useful error/warning https://github.com/OpenDataServices/flatten-tool/issues/442

## [0.24.2] - 2024-06-12

### Fixed
Expand Down
9 changes: 9 additions & 0 deletions flattentool/json_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import zc.zlibstorage
import ZODB.FileStorage

from flattentool.exceptions import DataErrorWarning
from flattentool.i18n import _
from flattentool.input import path_search
from flattentool.schema import make_sub_sheet_name
Expand Down Expand Up @@ -310,6 +311,14 @@ def parse(self):
# This is particularly useful for IATI XML, in order to not
# fall over on empty activity, e.g. <iati-activity/>
continue

if not isinstance(json_dict, dict):
warn(
_(f"The value at index {num} is not a JSON object"),
DataErrorWarning,
)
continue

self.parse_json_dict(json_dict, sheet=self.main_sheet)
# only persist every 2000 objects. peristing more often slows down storing.
# 2000 top level objects normally not too much to store in memory.
Expand Down
21 changes: 21 additions & 0 deletions flattentool/tests/test_json_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,27 @@ def test_parse_basic_json_dict():
assert parser.sub_sheets == {}


def test_parse_not_json_dict(recwarn):
parser = JSONParser(root_json_dict=[["test"], {"a": "b"}, "test"])
assert list(parser.main_sheet) == ["a"]
assert list(parser.main_sheet.lines) == [{"a": "b"}]
assert parser.sub_sheets == {}

assert len(recwarn) == 2

w = recwarn.pop(UserWarning)
assert (
repr(w.message)
== "DataErrorWarning('The value at index 0 is not a JSON object')"
)

w = recwarn.pop(UserWarning)
assert (
repr(w.message)
== "DataErrorWarning('The value at index 2 is not a JSON object')"
)


def test_parse_nested_dict_json_dict():
parser = JSONParser(
root_json_dict=[
Expand Down

0 comments on commit 72fe6ab

Please sign in to comment.