title: Convert an Array To a Nested JSON
body: <p>I am struggling the last days with an unresolved problem for me. I am stuck on this problem for long time that's why I took the decision to ask for your help.</p>
  <p>I'm reading an Excel  file and got the following array which I am now trying to convert it to a nested JSON to get accepted from the mongoDB but unfortunately I have difficulties to get there.</p>
  <pre class="lang-js prettyprint-override"><code>var arr = [&quot;number,isnewLanguage,label,numberOfChoices,languages/language,questions/mainText/language,questions/subText/language,questions/choices/language,questions/fields/langugage&quot;,
    &quot;1,false,label1,5,English,MAinText1,SubText1,choices1,false&quot;,
    &quot;2,false,label2,5,English,MAinText2,SubText2,choices2,false&quot;,
    &quot;3,true,label1,false,Italian,MainTextItalian1,SubTextItalian1,choicesItalian1,false&quot;,
    &quot;4,true,label2,false,Italian,MAinTextItalian2,SubTextItalian2,choicesItalian2,false&quot;
  ]
  </code></pre>
  <p>I would like to end up with the following structure. Here is a fiddle with my work <a href="https://jsfiddle.net/argusDob/3py6fd8m/2/" rel="nofollow noreferrer">https://jsfiddle.net/argusDob/3py6fd8m/2/</a></p>
  <pre class="lang-js prettyprint-override"><code>var json = [{
      number: 1,
      label1: &quot;label1&quot;,
      numberOfChoices: 5,
      languages: [{
          language: &quot;English&quot;,
          questions: [{
            mainText: &quot;MainText1&quot;,
            subText: &quot;subText1&quot;,
            choices: &quot;choices1&quot;,
            fields: &quot;false&quot;
          }]
        },
        {
          language: &quot;Italian&quot;,
          questions: [{
            mainText: &quot;MainTextItalian1&quot;,
            subText: &quot;subTextItlaian1&quot;,
            choices: &quot;choicesItalian1&quot;,
            fields: &quot;false&quot;
          }]
        }
      ]
    },
    {
      number: 2,
      label1: &quot;label2&quot;,
      numberOfChoices: 5,
      languages: [{
          language: &quot;English&quot;,
          questions: [{
            mainText: &quot;MainText2&quot;,
            subText: &quot;subText2&quot;,
            choices: &quot;choices2&quot;,
            fields: &quot;false&quot;
          }]
        },
        {
          language: &quot;Italian&quot;,
          questions: [{
            mainText: &quot;MainTextItalian2&quot;,
            subText: &quot;subTextItlaian2&quot;,
            choices: &quot;choicesItalian2&quot;,
            fields: &quot;false&quot;
          }]
        }
      ]
    }
  ]
  </code></pre>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var arr = ["number,isnewLanguage,label,numberOfChoices,languages/language,questions/mainText/language,questions/subText/language,questions/choices/language,questions/fields/langugage",
    "1,false,label1,5,English,MAinText1,SubText1,choices1,false",
    "2,false,label2,5,English,MAinText2,SubText2,choices2,false",
    "3,true,label1,false,Italian,MainTextItalian1,SubTextItalian1,choicesItalian1,false",
    "4,true,label2,false,Italian,MAinTextItalian2,SubTextItalian2,choicesItalian2,false"
  ]
  
  var attrs = arr.splice(0, 1);
  var test = [];
  
  var result = arr.map(function(row, idx) {
    var myObj = {};
    var array = [];
  
    var therows = row.split(",").slice(1);
    console.log(therows[0]);
    therows.forEach(function(value, i) {
  
      var myKeys = attrs[0].split(",").slice(1)
      var theKeys = myKeys[i]
  
      if (theKeys.split('/').length === 1) {
        myObj[theKeys] = value;
      }
  
      //languages
      var theLanguagesKey = theKeys.split("/")[0]
      if (theKeys.split("/").length == 2) {
        if (!myObj["languages"]) {
          var theNewLanguage = {}
          myObj["languages"] = [];
          theNewLanguage[theKeys.split('/')[1]] = value;
          myObj["languages"].push(theNewLanguage);
        }
      }
      //questions
      if (theKeys.split("/").length == 3) {
        if (!myObj["questions"]) {
          var theNewQuestions = {}
          myObj["questions"] = [];
          theNewQuestions[theKeys.split('/')[1]] = value;
          myObj["questions"].push(theNewQuestions);
        } else {
          for (var i = 0; i &lt; myObj["questions"].length; i++) {
            myObj["questions"][i][theKeys.split('/')[1]] = value;
          }
        }
  
      }
  
    })
    test.push(myObj)
  })
  console.log(test)</code></pre>
  </div>
  </div>
  </p>