Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten putting all list of list items in same object? #387

Open
odscjames opened this issue Aug 20, 2021 · 0 comments
Open

Flatten putting all list of list items in same object? #387

odscjames opened this issue Aug 20, 2021 · 0 comments

Comments

@odscjames
Copy link
Contributor

Setup

Start with a Schema:


{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "id": {
      "type": "string"
    },
    "address": {
      "type": "string"
    },
    "cats": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          },
          "good_quantities": {
            "type": "array",
            "items": {
              "type": "object",
              "properties": {
                "quantity": {
                  "type": "string"
                }
              }
            }
          }
        }
      },
      "rollUp": [
        "name"
      ]
    }
  }
}

Run:

flatten-tool create-template --main-sheet-name=homes --schema schema.json --rollup   -f csv -o testoutput

Look in testoutput.

We have a "homes.csv", and we are assuming that every home only has 1 cat (odd assumption but I'm simplifying from an more complex case so go with it)

We have a "cat_good_quantities.csv" - this is a separate child spreadsheet because clearly every cat has way more than 1 good quality (this assumption is true).

Edit "homes.csv":

id,address,cats/0/name
1,"5 Street","Bobby"

Edit "cat_good_quantities.csv":

id,cats/0/good_quantities/0/quantity
1,"Purrs"
1,"Furry"

Bobby is a good cat.

Run:

flatten-tool unflatten -o cats.json -f csv --schema schema.json  --root-list-path=homes  testoutput/

What you get


{
    "homes": [
        {
            "id": "1",
            "cats": [
                {
                    "good_quantities": [
                        {
                            "quantity": "Purrs"
                        }
                    ]
                },
                {
                    "good_quantities": [
                        {
                            "quantity": "Furry"
                        }
                    ]
                },
                {
                    "name": "Bobby"
                }
            ],
            "address": "5 Street"
        }
    ]
}

What I would like

{
    "homes": [
        {
            "id": "1",
            "cats": [
                {
                    "good_quantities": [
                        {
                            "quantity": "Purrs"
                        },
                        {
                            "quantity": "Furry"
                        }
                    ]
                    "name": "Bobby"
                }
            ],
            "address": "5 Street"
        }
    ]
}

Question

Is there any way to achieve this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant