1

I'm working in Javascript.

I'm trying to convert an array of arrays into a nested JSON object.

Here is the array of arrays. Each array represents a branch of a tree.

    LoL = [["parent", "child1", "grandchild1"],
["parent", "child1", "grandchild2"],
["parent","child2", "grandchild3"]]

I need to reformat this data into a nested JSON object, something like:

json = {
"name": "parent",
"children": [
    {
     "name": "child1",
      "children": [
         {
          "name": "grandchild1"
         },
         {
           "name": "grandchild2"
         }
       ]
    },
    {
      "name": "child2"
       "children":[
          {
           "name":"grandchild3"
          }
        ]
    }
]};

(essentially trying to turn an array of arrays to match the flare.json found in this project: http://bl.ocks.org/emeeks/4733217)

What would be the best approach to reformat?

2
  • Once you split the string by |, it is the same question. Here's a fiddle: jsfiddle.net/adigas/u16dnLro Commented Feb 7, 2020 at 6:08
  • @adigna Amazing! Thanks so much for taking the time for putting the fiddle together - it completely solved my problem! Commented Feb 7, 2020 at 6:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.