title: WCF Service accepts simple JSON string but rejects JSON set with 400 error
body: <p>I'm currently working on a PhoneGap app which communicates with our database via a WCF web service. </p>
  
  <p>I have made many requests to the server and retrieved data with no problem but in each case i was only passing in a couple of parameters as a JSON string which I created manually (no stringify). Now I have encountered a problem when trying to read from my local SQLite database and then send the information as JSON to the web service. The service is returning a 400 error with the following:  </p>
  
  <p>Error in deserializing body of request message for operation 'uploadData'. OperationFormatter encountered an invalid Message body. Expected to find an attribute with name 'type' and value 'object'. Found value 'array'.</p>
  
  <p>I am encoding the JSON as follows: </p>
  
  <pre><code>database.transaction(function(tx) {
      var query = "SELECT * FROM someTable WHERE something=something";
      tx.executeSql(query, [], function(tx, results) {
          var resultSet = new Array();
  
          for (i=0; i&lt;results.rows.length; i++) {
              var row = results.rows.item(i);
              resultSet[i] = row;         
          }
  
          var json = JSON.stringify(resultSet);
      }
  }
  </code></pre>
  
  <p>I then send the JSON string via an ajax request using jQuery:</p>
  
  <pre><code>$.ajax({
              type: "POST",
              url: "http://someurl/myService.svc/uploadData",
              contentType: "application/json; charset=utf-8",
              data: json,
              dataType: "json",
              success: function (data) {/*do something*/}, 
              error: function(jqXHR, textStatus, errorThrown) {
                  $('#test').html(textStatus + " - " + errorThrown + " - " + jqXHR.responseText);
              }
          }); 
  </code></pre>
  
  <p>My web service just keeps throwing back the same error. My question is this...How can I read from the local database and convert the result set into a usable JSON string to be sent via ajax.</p>
  
  <p>Thanks in advance. If more info needed or the problem is unclear please let me know and I'll do my best to provide more information.</p>
  
  <p><strong>EDIT:</strong> Tried removing the stringify but I'm still getting a 400 error. The error is a little different this time though:</p>
  
  <p>Error in deserializing body of request message for operation 'uploadData'. OperationFormatter encountered an invalid Message body. Encountered unexpected character 'u'.</p>
  
  <p><strong>EDIT:</strong> I've discovered that even a manually created JSON string that is perfectly valid still causes the error. It doesn't happen when sending a couple of parameters such as:</p>
  
  <pre><code>{"name" : "dean", "age" : 23}
  </code></pre>
  
  <p>It works perfectly fine. It's only rejecting sets of results such as:</p>
  
  <pre><code>[{"name" : "dean", "age" : 23},{"name" : "bob", "age" : 25}]
  </code></pre>
  
  <p>The method is defined in WCF interface as follows:</p>
  
  <pre><code> [OperationContract]
   [WebInvoke(Method = "POST",
            BodyStyle = WebMessageBodyStyle.Wrapped,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json)]
  
   string uploadData(string data);
  </code></pre>
  
  <p>It seems it's more of a problem with the WCF service than the JSON string itself.
  Does anyone know why WCF would accept a simple JSON string but reject a set? This is a major problem which has completely halted the development of the project so any help is greatly appreciated!</p>
  
  <p><strong>SOLVED:</strong>  Instead of passing the encoded JSON string directly I instead sent is as a value of another JSON string with key "data". So, as above I have my SQLite results as:</p>
  
  <pre><code>var json = JSON.stringify(resultSet);
  </code></pre>
  
  <p>I then pass it to the WCF service as follows:</p>
  
  <pre><code>...
  data: JSON.stringify({ data : json }),
  ...
  </code></pre>
  
  <p>And it now works fine.</p>