How to convert Data table to tree json format dynamically?










1















I'm using this data input to create a D3.js visualization:



Tree Layout



Okay so the right now my data input is a json file which i have hardcoded:




"name":"Fun",
"children": [

"name": "Legal",
"children": [
"name": "Adventure" ,
"name": "Movie" ,
"name": "M&m"
]
,

"name": "frowned upon",
"children": [
"name": "recreational stuff" ,
"name": "religious views"
]

]



But my data input is actually :



Data table input

How do i convert this data table dynamically to the above mentioned Json format, do i write a function which parses the data table to convert it into the above format or this is there another way.










share|improve this question




























    1















    I'm using this data input to create a D3.js visualization:



    Tree Layout



    Okay so the right now my data input is a json file which i have hardcoded:




    "name":"Fun",
    "children": [

    "name": "Legal",
    "children": [
    "name": "Adventure" ,
    "name": "Movie" ,
    "name": "M&m"
    ]
    ,

    "name": "frowned upon",
    "children": [
    "name": "recreational stuff" ,
    "name": "religious views"
    ]

    ]



    But my data input is actually :



    Data table input

    How do i convert this data table dynamically to the above mentioned Json format, do i write a function which parses the data table to convert it into the above format or this is there another way.










    share|improve this question


























      1












      1








      1








      I'm using this data input to create a D3.js visualization:



      Tree Layout



      Okay so the right now my data input is a json file which i have hardcoded:




      "name":"Fun",
      "children": [

      "name": "Legal",
      "children": [
      "name": "Adventure" ,
      "name": "Movie" ,
      "name": "M&m"
      ]
      ,

      "name": "frowned upon",
      "children": [
      "name": "recreational stuff" ,
      "name": "religious views"
      ]

      ]



      But my data input is actually :



      Data table input

      How do i convert this data table dynamically to the above mentioned Json format, do i write a function which parses the data table to convert it into the above format or this is there another way.










      share|improve this question
















      I'm using this data input to create a D3.js visualization:



      Tree Layout



      Okay so the right now my data input is a json file which i have hardcoded:




      "name":"Fun",
      "children": [

      "name": "Legal",
      "children": [
      "name": "Adventure" ,
      "name": "Movie" ,
      "name": "M&m"
      ]
      ,

      "name": "frowned upon",
      "children": [
      "name": "recreational stuff" ,
      "name": "religious views"
      ]

      ]



      But my data input is actually :



      Data table input

      How do i convert this data table dynamically to the above mentioned Json format, do i write a function which parses the data table to convert it into the above format or this is there another way.







      javascript json d3.js data-visualization






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 6:13









      Foo

      1




      1










      asked Nov 15 '18 at 6:07









      Shricharan ArumugamShricharan Arumugam

      457




      457






















          2 Answers
          2






          active

          oldest

          votes


















          1

















          var test = new function() 

          var table = [
          ['fun', 'legal', 'adventure'],
          ['fun', 'legal', 'mvie'],
          ['fun', 'legal', 'M&M'],
          ['fun', 'Frowned upon', 'Rec stuff'],
          ['fun', 'Frowned upon', 'Regligious views']
          ];

          var res = ;

          this.init = function()
          for (var i = 0; i < table.length; i++)
          var curRow = table[i];
          test.myAddFun(res, curRow, 0);

          console.log(res);
          ;

          this.myAddFun = function(_res, arr, startIndex)
          var addedToJSON = false;
          for (var i = 0; i < _res.length; i++)
          var curJSON = _res[i];
          if (curJSON['name'] == arr[startIndex])
          if (startIndex < arr.length - 1)
          test.myAddFun(curJSON['children'], arr, startIndex + 1);

          addedToJSON = true;
          break;


          if (addedToJSON)
          return;

          var curJSON = ;
          curJSON['name'] = arr[startIndex];
          if (startIndex < arr.length - 1)
          curJSON['children'] = ;
          test.myAddFun(curJSON['children'], arr, startIndex + 1);

          _res.push(curJSON);
          return;
          ;

          ;
          test.init();








          share|improve this answer






























            3














            You can write recursive function to restructure data.






            const a = [[
            'fun', 'legal', 'adventure',
            ], [
            'fun', 'legal', 'movie',
            ], [
            'fun', 'legal', 'm&m'
            ], [
            'fun', 'frowned upon', 'rec stuff'
            ], [
            'fun', 'frowned upon', 'religius views'
            ]];

            let t = ;

            const addLeaf = (array) => ;

            const addToTree = (tree, array) =>
            if (!array ;

            a.forEach((entry) =>
            t = addToTree(t, entry);
            );

            console.log(JSON.stringify(t, null, 2))








            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313396%2fhow-to-convert-data-table-to-tree-json-format-dynamically%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1

















              var test = new function() 

              var table = [
              ['fun', 'legal', 'adventure'],
              ['fun', 'legal', 'mvie'],
              ['fun', 'legal', 'M&M'],
              ['fun', 'Frowned upon', 'Rec stuff'],
              ['fun', 'Frowned upon', 'Regligious views']
              ];

              var res = ;

              this.init = function()
              for (var i = 0; i < table.length; i++)
              var curRow = table[i];
              test.myAddFun(res, curRow, 0);

              console.log(res);
              ;

              this.myAddFun = function(_res, arr, startIndex)
              var addedToJSON = false;
              for (var i = 0; i < _res.length; i++)
              var curJSON = _res[i];
              if (curJSON['name'] == arr[startIndex])
              if (startIndex < arr.length - 1)
              test.myAddFun(curJSON['children'], arr, startIndex + 1);

              addedToJSON = true;
              break;


              if (addedToJSON)
              return;

              var curJSON = ;
              curJSON['name'] = arr[startIndex];
              if (startIndex < arr.length - 1)
              curJSON['children'] = ;
              test.myAddFun(curJSON['children'], arr, startIndex + 1);

              _res.push(curJSON);
              return;
              ;

              ;
              test.init();








              share|improve this answer



























                1

















                var test = new function() 

                var table = [
                ['fun', 'legal', 'adventure'],
                ['fun', 'legal', 'mvie'],
                ['fun', 'legal', 'M&M'],
                ['fun', 'Frowned upon', 'Rec stuff'],
                ['fun', 'Frowned upon', 'Regligious views']
                ];

                var res = ;

                this.init = function()
                for (var i = 0; i < table.length; i++)
                var curRow = table[i];
                test.myAddFun(res, curRow, 0);

                console.log(res);
                ;

                this.myAddFun = function(_res, arr, startIndex)
                var addedToJSON = false;
                for (var i = 0; i < _res.length; i++)
                var curJSON = _res[i];
                if (curJSON['name'] == arr[startIndex])
                if (startIndex < arr.length - 1)
                test.myAddFun(curJSON['children'], arr, startIndex + 1);

                addedToJSON = true;
                break;


                if (addedToJSON)
                return;

                var curJSON = ;
                curJSON['name'] = arr[startIndex];
                if (startIndex < arr.length - 1)
                curJSON['children'] = ;
                test.myAddFun(curJSON['children'], arr, startIndex + 1);

                _res.push(curJSON);
                return;
                ;

                ;
                test.init();








                share|improve this answer

























                  1












                  1








                  1










                  var test = new function() 

                  var table = [
                  ['fun', 'legal', 'adventure'],
                  ['fun', 'legal', 'mvie'],
                  ['fun', 'legal', 'M&M'],
                  ['fun', 'Frowned upon', 'Rec stuff'],
                  ['fun', 'Frowned upon', 'Regligious views']
                  ];

                  var res = ;

                  this.init = function()
                  for (var i = 0; i < table.length; i++)
                  var curRow = table[i];
                  test.myAddFun(res, curRow, 0);

                  console.log(res);
                  ;

                  this.myAddFun = function(_res, arr, startIndex)
                  var addedToJSON = false;
                  for (var i = 0; i < _res.length; i++)
                  var curJSON = _res[i];
                  if (curJSON['name'] == arr[startIndex])
                  if (startIndex < arr.length - 1)
                  test.myAddFun(curJSON['children'], arr, startIndex + 1);

                  addedToJSON = true;
                  break;


                  if (addedToJSON)
                  return;

                  var curJSON = ;
                  curJSON['name'] = arr[startIndex];
                  if (startIndex < arr.length - 1)
                  curJSON['children'] = ;
                  test.myAddFun(curJSON['children'], arr, startIndex + 1);

                  _res.push(curJSON);
                  return;
                  ;

                  ;
                  test.init();








                  share|improve this answer
















                  var test = new function() 

                  var table = [
                  ['fun', 'legal', 'adventure'],
                  ['fun', 'legal', 'mvie'],
                  ['fun', 'legal', 'M&M'],
                  ['fun', 'Frowned upon', 'Rec stuff'],
                  ['fun', 'Frowned upon', 'Regligious views']
                  ];

                  var res = ;

                  this.init = function()
                  for (var i = 0; i < table.length; i++)
                  var curRow = table[i];
                  test.myAddFun(res, curRow, 0);

                  console.log(res);
                  ;

                  this.myAddFun = function(_res, arr, startIndex)
                  var addedToJSON = false;
                  for (var i = 0; i < _res.length; i++)
                  var curJSON = _res[i];
                  if (curJSON['name'] == arr[startIndex])
                  if (startIndex < arr.length - 1)
                  test.myAddFun(curJSON['children'], arr, startIndex + 1);

                  addedToJSON = true;
                  break;


                  if (addedToJSON)
                  return;

                  var curJSON = ;
                  curJSON['name'] = arr[startIndex];
                  if (startIndex < arr.length - 1)
                  curJSON['children'] = ;
                  test.myAddFun(curJSON['children'], arr, startIndex + 1);

                  _res.push(curJSON);
                  return;
                  ;

                  ;
                  test.init();








                  var test = new function() 

                  var table = [
                  ['fun', 'legal', 'adventure'],
                  ['fun', 'legal', 'mvie'],
                  ['fun', 'legal', 'M&M'],
                  ['fun', 'Frowned upon', 'Rec stuff'],
                  ['fun', 'Frowned upon', 'Regligious views']
                  ];

                  var res = ;

                  this.init = function()
                  for (var i = 0; i < table.length; i++)
                  var curRow = table[i];
                  test.myAddFun(res, curRow, 0);

                  console.log(res);
                  ;

                  this.myAddFun = function(_res, arr, startIndex)
                  var addedToJSON = false;
                  for (var i = 0; i < _res.length; i++)
                  var curJSON = _res[i];
                  if (curJSON['name'] == arr[startIndex])
                  if (startIndex < arr.length - 1)
                  test.myAddFun(curJSON['children'], arr, startIndex + 1);

                  addedToJSON = true;
                  break;


                  if (addedToJSON)
                  return;

                  var curJSON = ;
                  curJSON['name'] = arr[startIndex];
                  if (startIndex < arr.length - 1)
                  curJSON['children'] = ;
                  test.myAddFun(curJSON['children'], arr, startIndex + 1);

                  _res.push(curJSON);
                  return;
                  ;

                  ;
                  test.init();





                  var test = new function() 

                  var table = [
                  ['fun', 'legal', 'adventure'],
                  ['fun', 'legal', 'mvie'],
                  ['fun', 'legal', 'M&M'],
                  ['fun', 'Frowned upon', 'Rec stuff'],
                  ['fun', 'Frowned upon', 'Regligious views']
                  ];

                  var res = ;

                  this.init = function()
                  for (var i = 0; i < table.length; i++)
                  var curRow = table[i];
                  test.myAddFun(res, curRow, 0);

                  console.log(res);
                  ;

                  this.myAddFun = function(_res, arr, startIndex)
                  var addedToJSON = false;
                  for (var i = 0; i < _res.length; i++)
                  var curJSON = _res[i];
                  if (curJSON['name'] == arr[startIndex])
                  if (startIndex < arr.length - 1)
                  test.myAddFun(curJSON['children'], arr, startIndex + 1);

                  addedToJSON = true;
                  break;


                  if (addedToJSON)
                  return;

                  var curJSON = ;
                  curJSON['name'] = arr[startIndex];
                  if (startIndex < arr.length - 1)
                  curJSON['children'] = ;
                  test.myAddFun(curJSON['children'], arr, startIndex + 1);

                  _res.push(curJSON);
                  return;
                  ;

                  ;
                  test.init();






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 9:08









                  AbiAbi

                  11018




                  11018























                      3














                      You can write recursive function to restructure data.






                      const a = [[
                      'fun', 'legal', 'adventure',
                      ], [
                      'fun', 'legal', 'movie',
                      ], [
                      'fun', 'legal', 'm&m'
                      ], [
                      'fun', 'frowned upon', 'rec stuff'
                      ], [
                      'fun', 'frowned upon', 'religius views'
                      ]];

                      let t = ;

                      const addLeaf = (array) => ;

                      const addToTree = (tree, array) =>
                      if (!array ;

                      a.forEach((entry) =>
                      t = addToTree(t, entry);
                      );

                      console.log(JSON.stringify(t, null, 2))








                      share|improve this answer



























                        3














                        You can write recursive function to restructure data.






                        const a = [[
                        'fun', 'legal', 'adventure',
                        ], [
                        'fun', 'legal', 'movie',
                        ], [
                        'fun', 'legal', 'm&m'
                        ], [
                        'fun', 'frowned upon', 'rec stuff'
                        ], [
                        'fun', 'frowned upon', 'religius views'
                        ]];

                        let t = ;

                        const addLeaf = (array) => ;

                        const addToTree = (tree, array) =>
                        if (!array ;

                        a.forEach((entry) =>
                        t = addToTree(t, entry);
                        );

                        console.log(JSON.stringify(t, null, 2))








                        share|improve this answer

























                          3












                          3








                          3







                          You can write recursive function to restructure data.






                          const a = [[
                          'fun', 'legal', 'adventure',
                          ], [
                          'fun', 'legal', 'movie',
                          ], [
                          'fun', 'legal', 'm&m'
                          ], [
                          'fun', 'frowned upon', 'rec stuff'
                          ], [
                          'fun', 'frowned upon', 'religius views'
                          ]];

                          let t = ;

                          const addLeaf = (array) => ;

                          const addToTree = (tree, array) =>
                          if (!array ;

                          a.forEach((entry) =>
                          t = addToTree(t, entry);
                          );

                          console.log(JSON.stringify(t, null, 2))








                          share|improve this answer













                          You can write recursive function to restructure data.






                          const a = [[
                          'fun', 'legal', 'adventure',
                          ], [
                          'fun', 'legal', 'movie',
                          ], [
                          'fun', 'legal', 'm&m'
                          ], [
                          'fun', 'frowned upon', 'rec stuff'
                          ], [
                          'fun', 'frowned upon', 'religius views'
                          ]];

                          let t = ;

                          const addLeaf = (array) => ;

                          const addToTree = (tree, array) =>
                          if (!array ;

                          a.forEach((entry) =>
                          t = addToTree(t, entry);
                          );

                          console.log(JSON.stringify(t, null, 2))








                          const a = [[
                          'fun', 'legal', 'adventure',
                          ], [
                          'fun', 'legal', 'movie',
                          ], [
                          'fun', 'legal', 'm&m'
                          ], [
                          'fun', 'frowned upon', 'rec stuff'
                          ], [
                          'fun', 'frowned upon', 'religius views'
                          ]];

                          let t = ;

                          const addLeaf = (array) => ;

                          const addToTree = (tree, array) =>
                          if (!array ;

                          a.forEach((entry) =>
                          t = addToTree(t, entry);
                          );

                          console.log(JSON.stringify(t, null, 2))





                          const a = [[
                          'fun', 'legal', 'adventure',
                          ], [
                          'fun', 'legal', 'movie',
                          ], [
                          'fun', 'legal', 'm&m'
                          ], [
                          'fun', 'frowned upon', 'rec stuff'
                          ], [
                          'fun', 'frowned upon', 'religius views'
                          ]];

                          let t = ;

                          const addLeaf = (array) => ;

                          const addToTree = (tree, array) =>
                          if (!array ;

                          a.forEach((entry) =>
                          t = addToTree(t, entry);
                          );

                          console.log(JSON.stringify(t, null, 2))






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 15 '18 at 8:10









                          Delgee BDelgee B

                          16616




                          16616



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313396%2fhow-to-convert-data-table-to-tree-json-format-dynamically%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                              Syphilis

                              Darth Vader #20