How to convert tabbed tree to JSON in JavaScript?

I was looking for an answer, but I think this is some kind of strange question. How to convert to a text file using tabs for interval, this:

parent child child parent child grandchild grandhcild 

to

 { "name" : "parent", "children" : [ {"name" : "child"}, {"name" : "child"}, ] }, { "name" : "parent", "children" : [ { "name" : "child", "children" : [ {"name" : "grandchild"}, {"name" : "grandchild"}, {"name" : "grandchild"}, ] }, ] } 

JSON is probably not perfect, but hopefully makes my conclusion clear.

+7
json javascript jquery algorithm tabs
source share
2 answers

I just implemented this function for tabdown markup language - it does exactly what you were looking for, https://github.com/antyakushev/tabdown

Usage is quite simple:

 var lines = data.toString().split('\n'); var tree = tabdown.parse(lines); console.log(tree.toString()); 

You can also use the parsing function outside node.js, it does not depend on any modules.

+3
source share

Generate JSON from a table tree text file

Below is a link to your concerns. All you have to do is update the code so that the output is formatted according to your requirements.

  • Python file parsing: building a tree from a text file
  • Creating a tree / deeply nested dict from an indented text file in python
  • Parse Tree is a Ruby example for parsing a tab tree. Created for #ruby on FreeNode.

Tab separator for JSON

Other help

  • How to convert tsv to json
  • Convert comma separated list to JSON using Javascript
-2
source share

All Articles