title: Dynamic build JSON variable from HTML5 mobile send to Ruby on Rails controller
body: <p>Client: HTML5 + JS + localdb
  Server: Rails3</p>
  
  <p>I'm trying to code small mobile app: the  app if a small form that records some contacts info into a local database.
  Then when clicking on a Sync button it send the contacts to a RoR app.</p>
  
  <p>Trying to follow some best practise I would like to store the data on JSON format before sending it to the RoR app.
  The JSON variable is building dynamically from a localdb:</p>
  
  <p>on client side:</p>
  
  <pre><code>var myJSON= { 
              jcontacts:[]
          }; 
  
  ...
  // loop on localdb
  for (var i=0; i&lt; vContacts.length;i++)
  {                               
      myJSON.jcontacts.push({
          "name"      : vContacts[i][0],
          "email"     : vContacts[i][1],
          "info"      : vContacts[i][2]
      });                                                                                     
  }           
  var myJSONText = JSON.stringify(myJSON);    //(1) convert array to JSON string                                              
  
  /*
  console.log(myJSONText) --&gt; {"jcontacts":[{"name":"John","email":"john@email.com","info":"Owner"},
  {"name":"Mary","email":"mary@gmail.com","info":"Doctor"},
  {"name":"Gary","email":"gary@email.com","info":"Driver"}]}
  */
  
  var myObject = JSON.parse(myJSONText);// (2) convert JSON string to JSON object, just do it to make sure my var is valid JSON format
  myGlobalJSONText = JSON.stringify(myObject); // (3) convert JSON object to JSON string before sending to server
  
  /*
  console.log(myGlobalJSONText) --&gt; {"jcontacts":[{"name":"John","email":"john@email.com","insurance":"Medical"},
  {"name":"Mary","email":"mary@gmail.com","insurance":"Medical"},
  {"name":"Gary","email":"gary@email.com","insurance":"Car"}]}
  */
  ...
  // Ajax
     $.ajax({
          type: "POST",                    
          url : "http://localhost:3000/contacts/multinew",
          cache: false,
          data: myGlobalJSONText,
          success: onSuccess,
          error: onError
      });
  </code></pre>
  
  <p>On Rails side:</p>
  
  <pre><code>def multinew        
      @vcontacts = JSON.parse(params[:jcontacts])     
      @vcontacts.each {|x| Contact.create(x)}   
  
      respond_to do |format|
          format.html { redirect_to(@contact)}
        format.xml  { head :ok }
      end
   end
  </code></pre>
  
  <p>When sending the data, my Rails server replies:</p>
  
  <pre><code>Started POST "/contacts/multinew" for 127.0.0.1 at 2011-04-25 20:49:23 +0700
    Processing by ContactsController#multinew as */*
    Parameters: {"{\"jcontacts\":"=&gt;{"{\"name\":\"John\",\"email\":\"john@email.com\",\"info\":\"Owner\"},{\"name\":\"Mary\",\"email\":\"mary@gmail.com\",\"info\":\"Doctor\"},{\"name\":\"Gary\",\"email\":\"gary@email.com\",\"info\":\"Driver\"}"=&gt;{"}"=&gt;nil}}}
  Completed   in 0ms
  
  TypeError (can't convert nil into String):
  </code></pre>
  
  <p>So my questions are:</p>
  
  <p>1) before trying to fix the Rails part I would like to know if everything is correct on the client javascript code?
  My assumption using JSON is that if a variable is properly formed on client side (using JSON.stringify) it should be interpreted correctly on server side (using JSON.parse), this is the reason why people use JSON?</p>
  
  <p>2) if the client code side is correct it means I should fixe the ruby code by putting some regex processing?
  Is this case I cannot see the advantage of using JSON (others the security reason)</p>
  
  <p>Many thanks!</p>