title: How do I build a JSON object to send to an AJAX WebService?
body: <p>After trying to format my JSON data by hand in javascript and failing miserably, I realized there's probably a better way.  Here's what the code for the web service method and relevant classes looks like in C#:</p>
  
  <pre><code>[WebMethod]
  public Response ValidateAddress(Request request)
  {
      return new test_AddressValidation().GenerateResponse(
          test_AddressValidation.ResponseType.Ambiguous);
  }
  
  ...
  
  public class Request
  {
      public Address Address;
  }
  
  public class Address
  {
      public string Address1;
      public string Address2;
      public string City;
      public string State;
      public string Zip;
      public AddressClassification AddressClassification;
  }
  
  public class AddressClassification
  {
      public int Code;
      public string Description;
  }
  </code></pre>
  
  <p>The web service works great with using SOAP/XML, but I can't seem to get a valid response using javascript and jQuery because the message I get back from the server has a problem with my hand-coded JSON.  </p>
  
  <p>I can't use the jQuery <code>getJSON</code> function because the request requires HTTP POST, so I'm using the lower-level <code>ajax</code> function instead:</p>
  
  <pre><code>$.ajax({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      url: "http://bmccorm-xp/HBUpsAddressValidation/AddressValidation.asmx/ValidateAddress",
      data: "{\"Address\":{\"Address1\":\"123 Main Street\",\"Address2\":null,\"City\":\"New York\",\"State\":\"NY\",\"Zip\":\"10000\",\"AddressClassification\":null}}",
      dataType: "json",
      success: function(response){
          alert(response);
      }
  })
  </code></pre>
  
  <p>The <code>ajax</code> function is submitting everything specified in <code>data:</code>, which is where my problem is.  How do I build a properly formatted JSON object in javascript so I can plug it in to my <code>ajax</code> call like so: </p>
  
  <pre><code>data: theRequest
  </code></pre>
  
  <p>I'll eventually be pulling data out of text inputs in forms, but for now hard-coded test data is fine.  </p>
  
  <p><strong>How do I build a properly formatted JSON object to send to the web service?</strong></p>
  
  <hr>
  
  <p><em>UPDATE: It turns out that the problem with my request wasn't the formatting of the JSON, as T.J. pointed out, but rather that my JSON text didn't conform to requirements of the web service.  Here's a valid JSON request based on the code in the WebMethod:</em></p>
  
  <pre><code>'{"request":{"Address":{"Address1":"123 Main Street","Address2":"suite 20","City":"New York","State":"NY","Zip":"10000","AddressClassification":null}}}'
  </code></pre>
  
  <p>This brought up another question: <a href="https://stackoverflow.com/questions/2738321/when-is-case-sensitivity-important-in-json-requests-to-asp-net-web-services-asmx">When is case sensitivity important in JSON requests to ASP.NET web services (ASMX)?</a></p>