Skip to content

Convert from json to (flattened) csv #3

@rogeriomarques

Description

@rogeriomarques

Some systems won't accept json as an input. I've tried several json-to-csv tools and none of then handle nested json arrays in a meaningful manner, creating rows only for the first level instead of doing it bottom-to-top. Consider:

Input:

An array with an arbitrary number of elements that in turn can have and arbitrary number of arrays, recursively.

{
   "trees": [
      {
         "id": 1,
         "name": "tree1",
         "branches": [
            {
               "id": 1,
               "name": "branch1",
               "leaves": [
                  {
                     "id": 1,
                     "name": "leave1"
                  },
                  {
                     "id": 2,
                     "name": "leave2"
                  }
               ]
            },
            {
               "id": 2,
               "name": "branch2",
               "leaves": [
                  {
                     "id": 1,
                     "name": "leave1"
                  },
                  {
                     "id": 2,
                     "name": "leave2"
                  }
               ]
            }
         ]
      }
   ]
}

Output:

Each leave becomes a csv row with all it's ancestor's fields. The example above would result in four rows with the following fields:

  • leaves.id
  • leaves.name
  • branchs.id
  • branchs.name
  • trees.id
  • trees.name

Caveat:

It may be necessary to limit the allowed tree's depth to preserve resources.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions