Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json_input: If json_dict is not a dict, return a useful error/warning #448

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

## [0.25.0] - 2024-07-05

### Fixed

- Don't error when there's a datetime in the header https://github.com/OpenDataServices/flatten-tool/issues/438

- 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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def run(self):

setup(
name="flattentool",
version="0.24.2",
version="0.25.0",
author="Open Data Services",
author_email="[email protected]",
packages=["flattentool"],
Expand Down
Loading