Skip to content

Commit c07d154

Browse files
author
James (ODSC)
authored
Merge pull request #401 from OpenDataServices/flatten-array-of-arrays
json_input: Add support for flattening an array of arrays
2 parents b5619d6 + c43ffc8 commit c07d154

File tree

6 files changed

+38
-3
lines changed

6 files changed

+38
-3
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88

9+
### Added
10+
11+
## [0.18.0] - 2022-09-26
12+
13+
- Add support for flattening an array of arrays https://github.com/OpenDataServices/flatten-tool/issues/398
14+
915
## [0.17.2] - 2022-06-15
1016

1117
### Fixed

flattentool/json_input.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,10 +419,20 @@ def parse_json_dict(
419419
# Check for an array of BASIC types
420420
# TODO Make this check the schema
421421
# TODO Error if the any of the values contain the separator
422-
# TODO Support doubly nested arrays
423422
flattened_dict[sheet_key(sheet, parent_name + key)] = ";".join(
424423
map(str, value)
425424
)
425+
# Arrays of arrays
426+
elif all(
427+
l not in BASIC_TYPES
428+
and not hasattr(l, "items")
429+
and hasattr(l, "__iter__")
430+
and all(type(x) in BASIC_TYPES for x in l)
431+
for l in value
432+
):
433+
flattened_dict[sheet_key(sheet, parent_name + key)] = ";".join(
434+
map(lambda l: ",".join(map(str, l)), value)
435+
)
426436
else:
427437
if (
428438
self.rollup and parent_name == ""

flattentool/tests/fixtures/360-giving-schema.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,13 @@
623623
"description": "A web link pointing to the source of this data. This may be an original 360Giving data file, a file from which the data was converted, or an organisation website.",
624624
"weight": 25,
625625
"title": "Data Source"
626+
},
627+
"test_array_of_arrays": {
628+
"type": "array",
629+
"items": {
630+
"type": "array",
631+
"items": {"type": "string"}
632+
}
626633
}
627634
}
628635
}

flattentool/tests/fixtures/fundingproviders-grants_fixed_2_grants.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
],
3636
"Grant type": "Large Awards",
3737
"Full name of applicant": "Miss Jane Roe",
38-
"awardDate": "24/07/2014"
38+
"awardDate": "24/07/2014",
39+
"test_array_of_arrays": [["a", "r"], ["s", "t", "n"], ["e"]]
3940
},
4041
{
4142
"recipientOrganization": [

flattentool/tests/test_json_input.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ def test_parse_array():
152152
assert parser.sub_sheets == {}
153153

154154

155+
def test_parse_array_of_arrays():
156+
parser = JSONParser(
157+
root_json_dict=[
158+
OrderedDict([("testarray", [["item", "anotheritem", 42], ["a", "b", 1]])])
159+
]
160+
)
161+
assert list(parser.main_sheet) == ["testarray"]
162+
assert list(parser.main_sheet.lines) == [{"testarray": "item,anotheritem,42;a,b,1"}]
163+
assert parser.sub_sheets == {}
164+
165+
155166
def test_root_list_path():
156167
parser = JSONParser(
157168
root_json_dict={

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def run(self):
4343

4444
setup(
4545
name="flattentool",
46-
version="0.17.2",
46+
version="0.18.0",
4747
author="Open Data Services",
4848
author_email="[email protected]",
4949
packages=["flattentool"],

0 commit comments

Comments
 (0)