Skip to content

Commit b7c9a88

Browse files
authored
Merge pull request #356 from OpenDataServices/docs-update-kca
Adding array-handling info
2 parents 5a9a25f + b1b8fa2 commit b7c9a88

File tree

9 files changed

+69
-7
lines changed

9 files changed

+69
-7
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ install:
1212
- if [[ $TRAVIS_PYTHON_VERSION != 3.5 ]]; then pip install black==19.10b0; fi
1313
script:
1414
- if [[ $TRAVIS_PYTHON_VERSION != 3.5 ]]; then black --check *.py */; fi
15-
- isort --check-only --recursive *.py */
15+
# isort 5 only runs under Python >= 3.6
16+
- if [[ $TRAVIS_PYTHON_VERSION != 3.5 ]]; then isort --check-only --recursive *.py */; fi
1617
- flake8
1718
- py.test --cov .
1819
after_success: coveralls

docs/unflatten.rst

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,10 +475,10 @@ section.
475475
Plain Lists (Unsupported)
476476
-------------------------
477477

478-
Flatten Tool doesn't support arrays of JSON values other than objects (just
479-
described in the previous section).
478+
Flatten Tool doesn't recognise arrays of JSON values other than objects (just
479+
described in the previous section) unless a schema is used.
480480

481-
As a result heading names such as ``tag/0`` and ``tag/1`` would be ignored and an
481+
Heading names such as ``tag/0`` and ``tag/1`` would be ignored and an
482482
empty array would be put into the JSON.
483483

484484
Here's some example data:
@@ -494,6 +494,26 @@ And the result:
494494
.. literalinclude:: ../examples/cafe/plain-list/expected.json
495495
:language: json
496496

497+
However, an array of tags in the following format (semi-colon separated) can be handled if a schema is passed to Flatten Tool specifying the array type of the field.
498+
499+
.. csv-table::
500+
:file: ../examples/cafe/plain-list-schema/data.csv
501+
:header-rows: 1
502+
503+
The schema we'll pass is:
504+
505+
.. literalinclude:: ../examples/cafe/plain-list-schema/tagsArraySchema.json
506+
:language: json
507+
508+
And the result:
509+
510+
.. literalinclude:: ../examples/cafe/plain-list-schema/cmd.txt
511+
:language: bash
512+
.. literalinclude:: ../examples/cafe/plain-list-schema/expected.json
513+
:language: json
514+
515+
Read on for more about typed fields and use of schemas.
516+
497517

498518
Typed fields
499519
============
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
$ flatten-tool unflatten -f=csv --root-list-path=cafe --schema=examples/cafe/plain-list-schema/tagsArraySchema.json examples/cafe/plain-list-schema/
2+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name,tags
2+
Healthy Cafe,health;low-cost;locally sourced food;take-out
3+
Vegetarian Cafe,veggie
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"cafe": [
3+
{
4+
"name": "Healthy Cafe",
5+
"tags": [
6+
"health",
7+
"low-cost",
8+
"locally sourced food",
9+
"take-out"
10+
]
11+
},
12+
{
13+
"name": "Vegetarian Cafe",
14+
"tags": [
15+
"veggie"
16+
]
17+
}
18+
]
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-04/schema#",
3+
"type": "object",
4+
"properties": {
5+
"tags": {
6+
"items": {
7+
"type": "string"
8+
},
9+
"type": "array"
10+
},
11+
"name": {
12+
"type": "string"
13+
}
14+
}
15+
}
16+

flattentool/cli.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from flattentool.json_input import BadlyFormedJSONError
1010
from flattentool.output import FORMATS as OUTPUT_FORMATS
1111

12-
1312
"""
1413
This file does most of the work of the flatten-tool commandline command.
1514

flattentool/schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
if sys.version_info[:2] > (3, 0):
1616
import pathlib
1717
else:
18-
import urlparse, urllib
18+
import urllib
19+
20+
import urlparse
1921

2022

2123
def get_property_type_set(property_schema_dict):

flattentool/tests/test_docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def test_example_in_doc(root, filename):
133133

134134

135135
def test_expected_number_of_examples_in_docs_data():
136-
assert len(examples_in_docs_data) == 56
136+
assert len(examples_in_docs_data) == 57
137137

138138

139139
def _simplify_warnings(lines):

0 commit comments

Comments
 (0)