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

Convert from json to (flattened) csv #3

Closed
rogeriomarques opened this issue Mar 26, 2016 · 3 comments
Closed

Convert from json to (flattened) csv #3

rogeriomarques opened this issue Mar 26, 2016 · 3 comments

Comments

@rogeriomarques
Copy link

@rogeriomarques rogeriomarques commented Mar 26, 2016

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.

@martindrapeau
Copy link
Collaborator

@martindrapeau martindrapeau commented Mar 28, 2016

This is an good first implementation to do. Ultimately though, is this what people expect for JSON to CSV conversion?

@martindrapeau
Copy link
Collaborator

@martindrapeau martindrapeau commented Jan 24, 2018

I've implemented JSON to CSV and a flattening option. Have a look:
http://www.csvjson.com/json2csv/4c25f831d3ada128be0ee8829467b447

Let me know if this is what you had in mind.

@rogeriomarques
Copy link
Author

@rogeriomarques rogeriomarques commented Sep 13, 2018

Thank you so much, this is exactly what I was suggesting and comes handy for my research project. Chapeau! (and sorry for not seen it before)

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.