Skip to content

Commit ecff851

Browse files
committed
Adding array-handinling info
1 parent 5a9a25f commit ecff851

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

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+

0 commit comments

Comments
 (0)