title: Saving CR/LF in json data
body: <p>I am saving table data to a json object. The table data is coming from txt inputs and textareas in the table cells.</p>
  
  <p>I'm running into a problem with CR/LF characters in the JSON elements holding the textarea data. The JSON data gets saved to the database fine, but when I pass it back to the jQuery function that populates the table using that data, I get this: </p>
  
  <pre><code>SyntaxError: JSON.parse: bad control character in string literal at line 1 column 67 of the JSON data
  var array = JSON.parse(notes),
  </code></pre>
  
  <p>in the console.</p>
  
  <p>I put the JSON data in Notepad++ with Show All Characters on and the CR/LF was at column 67.</p>
  
  <p>Here's a sample of JSON data that I'm working with:</p>
  
  <pre><code>[["","","",""],["","9/23/14","",""],["","30789 detail, x_vendor_no**CR/LF HERE**
  20597 header","",""],["","99 del invalid x_vendor_no","",""],["","30780","",""],["","","",""],["","","",""],["","","",""]]
  </code></pre>
  
  <p>Is there a way to allow CR/LF in the data?</p>
  
  <p><strong>UPDATE</strong>
  11684's suggestion to use replace to remove the \r part of the CRLF won't work. Here's why:</p>
  
  <p>Here's the complete function that uses the JSON data:
  <em>(Updated to work with Update #2 code below)</em></p>
  
  <pre><code>function PopulateTaskTableWithNotes(tableID,notesArray) {
      // JSON parse removed per answer suggestion
      var r, c, note;
  
      for (r = 0; r &lt; notesArray.length; ++r) {
          for (c = 0; c &lt; notesArray[r].length; ++c) {
              note = notesArray[r][c];
          $('#' + tableID + ' tr:nth-child(' + (r + 1) + ') td:nth-child(' + (c + 1) + ')').children(':first-child').val(note);
          }
      }
  }
  </code></pre>
  
  <p>I still get the error on the line that tries to parse the JSON data. The replace function apparently can't "find" characters within an array element.</p>
  
  <p><strong>UPDATE #2</strong>
  Here's how I am creating the array:</p>
  
  <pre><code>var siteID = $('#ddlUserSites option:selected').val(),
      numRows = $('#' + tableID + ' tr').length,
      numCols = $('#' + tableID).find('tr:first th').length,
      notesArray = new Array(numRows),
      rowNum = 1,
      note = '',
      colNum;
  
  while (rowNum &lt;= numRows) {
      notesArray[rowNum] = new Array(numCols);
  
      // Reset colNum for next row iteration
      colNum = 1;
      while (colNum &lt;= numCols) {
          note = '';
  
          if ($('#' + tableID + ' tr:nth-child(' + rowNum + ') td:nth-child(' + colNum + ')').children(':first-child').is('input,textarea')) {
              note = $('#' + tableID + ' tr:nth-child(' + rowNum + ') td:nth-child(' + colNum + ')').children(':first-child').val();
          }
          notesArray[rowNum][colNum] = note;
          //console.log('Note for rowNum ' + rowNum + ', colNum ' + colNum + ': ' + note);
          colNum++;
      }
      // Remove first element in current row array
      notesArray[rowNum].shift();
      rowNum++;
  }
  // Remove first element in array
  notesArray.shift();
  JSON.stringify(notesArray); // Added per an answer here
  console.log('Final notesArray: ' + $.toJSON(notesArray));
  
  $.ajax({
      data: {saveTaskNotes: 'true', userID:userID, siteID:siteID, taskTable:tableID, notes:notesArray},
      success: function(data) {
          console.log('Save task notes data: ' + data);
      }
  });
  </code></pre>
  
  <p>The "Final notesArray" console output looks fine, but now, with stringify added, the function above (PopulateTaskTableWithNotes) console output shows that it's reading through every character in the array as a separate element!</p>
  
  <p>Maybe this will help too, as far as what's happening to the data between the creating and reading functions: the array is being saved to a single MySQL database field and then retrieved for the PopulateTable function via $.ajax() (on both ends).</p>
  
  <p>Having said that, do I need to look at what I'm doing with/to the array in the PHP code?</p>
  
  <p><strong>UPDATE #3</strong>
  Here's the PHP function that takes the data in and writes to the MySQL db:</p>
  
  <pre><code>function SaveTaskNotes($userID,$siteID,$taskTable,$notes) {
      $notes = json_encode($notes);
      $insertUpdateTaskNotesResult = '';
      $insertTaskNotes = "INSERT INTO userProgress (userProgressUserID,userProgressSiteID,userProgressNotesTable,userProgressNotes) values ($userID,$siteID,'" . $taskTable . "','" . $notes . "')";
      $log-&gt;lwrite('$insertTaskNotes: ' . $insertTaskNotes);
      $resultInsertTaskNotes = @mysqli_query($dbc,$insertTaskNotes);
      if ($resultInsertTaskNotes) {
          $insertUpdateTaskNotesResult = 'insertTaskNotesSuccess';
      } else {
          if (mysqli_error($dbc) != '') {
              $log-&gt;lwrite('INSERT TASK NOTES: An error occurred while attempting to add the task notes. Query: ' . $insertTaskNotes . ', mysqli_error: ' . mysqli_error($dbc));
          }
          $insertUpdateTaskNotesResult = 'insertTaskNotesFail';
      }
      echo $insertUpdateTaskNotesResult;
  }
  </code></pre>
  
  <p>And here's the function that gets the data from the db and sends it to the above $.ajax function:</p>
  
  <pre><code>function GetUserTaskNotes($userID,$siteID,$taskTableID) {
      $queryGetUserTaskNotes = "SELECT userProgressNotes FROM userProgress WHERE userProgressUserID = $userID AND userProgressSiteID = $siteID AND userProgressNotesTable = '" . $taskTableID . "'";
      $log-&gt;lwrite('$queryGetUserTaskNotes: ' . $queryGetUserTaskNotes);
      $resultGetUserTaskNotes = @mysqli_query($dbc,$queryGetUserTaskNotes);
      if ($resultGetUserTaskNotes) {
          $taskNotes = mysqli_fetch_assoc($resultGetUserTaskNotes);
          $log-&gt;lwrite('Retrieved $taskNotes[\'userProgressNotes\']: ' . $taskNotes['userProgressNotes']);
          echo $taskNotes['userProgressNotes'];
      } else {
          if (mysqli_error($dbc) != '') {
              $log-&gt;lwrite('GET TASK NOTES: An error occurred while attempting to retrieve the task notes. Query: ' . $queryGetUserTaskNotes . ', mysqli_error: ' . mysqli_error($dbc));
          }
          echo 'getTaskNotesFail';
      }
  }
  </code></pre>
  
  <p>In both the save and get functions the $log output shows that the array never changes (with the above js/php code) and pasting the array in to notepad++ shows that the CR/LF is still there throughout.</p>