title: How to convert Plain text to Json Object in JavaScript
body: <p>How to convert plain text into JSON value in Javascript. I tried lot. But unable to fetch as expected. Kindly anybody help</p>
  
  <p>Actual String getting from command line which is executing command from javascript</p>
  
  <pre><code>Net State: OFFLINE
  Net Err:
  Net Time: 0
  
  Current Sample: 0
  
  Sample #0
          Sample Role: OFFLINE
          Sample Status: DISABLED
          Sample Errors: ERROR STATUS
          Test Mode: FAILED
          Test Status: NOT RUNNING      
  
  Sample #1
          Sample Role: ONLINE
          Sample Status: ENABLED
          Sample Errors: NO ERROR
          Test Mode: ENABLED
          Test Status: RUNNING
  </code></pre>
  
  <p>Expected JSON value</p>
  
  <pre><code>{
    "Net_State": "OFFLINE",
    "Net_Err": "",
    "Net_Time": 0,
    "Current_sample": 0,
    "Sample": [
          {
        "Sample_Role": "OFFLINE",
        "Sample_Status": "DISABLED",
        "Sample_Errors": "ERROR STATUS",
        "Test_Mode": "FAILED",
        "Test_Status": "NOT RUNNING"
      },
      {
        "Sample_Role": "ONLINE",
        "Sample_Status": "ENABLED",
        "Sample_Errors": "NO ERROR",
        "Test_Mode": "ENABLED",
        "Test_Status": "RUNNING"
      }
    ]
  }
  </code></pre>
  
  <p>I tried the following code, But how can i write it shortly</p>
  
  <pre><code>var res = response.split(/\r?\n/);
  res = res.filter(function(e){return e});
  var myObject = {
  node : []
  };
  var nodeObject =[];
  var i =0, j=0, k = 0, l=0;
  var sampleArr = []
  var loop = false;
  for (var i = 0; i &lt; res.length; i++) {
  var itemValue = res[i]
  if (itemValue.includes(':')) {
          var sp = itemValue.split(':');
          if (!loop) {
          myObject[sp.shift().trim()] = sp.join(':').trim();
      } else {
          if(nodeObject.length &gt; 0) {
              if (j ==0) {
                  myObject.node[l] = nodeObject;
                  nodeObject = [];
                  l = l+1;
              }
          }
  
          nodeObject.push({[sp.shift().trim()] : sp.join(':').trim()});
          j = j+1;
          if (k &gt; 0 &amp;&amp; k == j) {
          myObject.node[l] = nodeObject;
          nodeObject = [];
          }
      }
  } else {
      loop = true;
      k=j;        
      j = 0;
  }
  }     
  </code></pre>
  
  <p>Thanks in advance.</p>