Examples

Simple example

The JSON simple.json:

{
    "main": [
        {
            "a": {
                "b": "1",
                "c": "2"
            },
            "d": "3"
        },
        {
            "a": {
                "b": "4",
                "c": "5"
            },
            "d": "6"
        }
    ]
}

Can be converted to/from a spreadsheet like simple/main.csv:

a/b a/c d
1 2 3
4 5 6

Using the commands:

flatten-tool unflatten -f csv examples/simple -o examples/simple.json
flatten-tool flatten -f csv examples/simple.json -o examples/simple

One to many relationships (JSON arrays)

There are multiple shapes of spreadsheet that can be used to produce the same JSON arrays. E.g. to produce this JSON:

{
    "main": [
        {
            "id": "1",
            "d": "6",
            "a": [
                {
                    "b": "2",
                    "c": "3"
                },
                {
                    "b": "4",
                    "c": "5"
                }
            ]
        },
        {
            "id": "7",
            "d": "12",
            "a": [
                {
                    "b": "8",
                    "c": "9"
                },
                {
                    "b": "10",
                    "c": "11"
                }
            ]
        }
    ]
}

We can use these Spreadsheets:

id a/0/b a/0/c
1 2 3
1 4 5
7 8 9
7 10 11
id d
1 6
7 12

These are also the spreadsheets that flatten-tool's flatten (JSON to Spreadsheet) will produce.

Commands used to generate this:

flatten-tool unflatten -f csv examples/array_multisheet -o examples/array_multisheet.json
flatten-tool flatten -f csv examples/array.json -o examples/array_multisheet

However, there are other "shapes" of spreadsheet that can produce the same JSON.

New columns for each item of the array:

id a/0/b a/0/c a/1/b a/1/c d
1 2 3 4 5 6
7 8 9 10 11 12
flatten-tool unflatten -f csv examples/array_pointer -o examples/array.json

Repeated rows:

id a/0/b a/0/c d
1 2 3 6
1 4 5 6
7 8 9 12
7 10 11 12
flatten-tool unflatten -f csv examples/array_repeat_rows -o examples/array.json

Arrays within arrays

{
    "main": [
        {
            "id": "1",
            "d": "6",
            "a": [
                {
                    "id": "2",
                    "b": [
                        {
                            "c": "3"
                        },
                        {
                            "c": "3a"
                        }
                    ]
                },
                {
                    "id": "4",
                    "b": [
                        {
                            "c": "5"
                        },
                        {
                            "c": "5a"
                        }
                    ]
                }
            ]
        },
        {
            "id": "7",
            "d": "12",
            "a": [
                {
                    "id": "8",
                    "b": [
                        {
                            "c": "9"
                        },
                        {
                            "c": "9a"
                        }
                    ]
                },
                {
                    "id": "10",
                    "b": [
                        {
                            "c": "11"
                        },
                        {
                            "c": "11a"
                        }
                    ]
                }
            ]
        }
    ]
}
id a/0/id
1 2
1 4
7 8
7 10
id d
1 6
7 12