Skip to content
Open
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
5 changes: 4 additions & 1 deletion ianalyzer_readers/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,14 +619,17 @@ class JSON(Extractor):
When working with nested lists, use JSONReader to unnest.

Parameters:
keys (Iterable[str]): the keys with which to retrieve a field value from the source
keys (Iterable[str]): the keys with which to retrieve a field value from the
source. If empty, this returns the source as-is.
'''

def __init__(self, *keys, **kwargs):
self.keys = list(keys)
super().__init__(**kwargs)

def _apply(self, data: Union[str, dict], key_index: int = 0, **kwargs) -> str:
if not len(self.keys):
return data
key = self.keys[key_index]
data = data.get(key)
if len(self.keys) > key_index + 1:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_json_reader.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from tests.json.json_reader import JSONDocumentReader, JSONMultipleDocumentReader
from ianalyzer_readers.readers.core import Field
from ianalyzer_readers.extract import JSON

expected = [
{
Expand Down Expand Up @@ -41,3 +43,10 @@ def _assert_matches(target: dict, doc: dict):
assert len(target.keys()) == len(doc.keys())
for key in target.keys():
assert doc.get(key) == target.get(key)

def test_json_no_key():
reader = JSONDocumentReader()
field = Field('data', extractor=JSON())
reader.fields.append(field)
doc = next(reader.documents())
assert 'TITLE' in doc['data']