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 · 4 comments
Closed

Convert from json to (flattened) csv #3

rogeriomarques opened this issue Mar 26, 2016 · 4 comments

Comments

@rogeriomarques
Copy link

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

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

@martindrapeau
Copy link
Collaborator

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 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)

@osdotme-owner
Copy link

Thanks @rogeriomarques for building this wonderful tool. I tried it for the scenario described above & it works beautifully. But I also tried reversing it by feeding CSV as given below at https://csvjson.com/csv2json. I was checking if I can get original json back but it did not generate original json back.

I am not a developer but your service is super handy for scenarios where some website provides tools to export & import json but between export & import some hand editing is needed. A CSV makes it much easier to edit in any popular spreadsheet and if your tool can convert it back, this can be pretty nice. Just a suggestion.

"trees.id","trees.name","trees.branches.id","trees.branches.name","trees.branches.leaves.id","trees.branches.leaves.name"
1,"tree1",1,"branch1",1,"leave1"
1,"tree1",1,"branch1",2,"leave2"
1,"tree1",2,"branch2",1,"leave1"
1,"tree1",2,"branch2",2,"leave2"

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

3 participants