function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
debiken88debiken88 

Quick question on JSON deserialize

Hi all,
      This is my first attempt at using JSON in APEX, seems pretty straight forward.  I have what I think is a very simple question...making a call out to a webservice that is returning a JSON string for new order information.   In my apex class, I have a simple order class:
public class OrderInfo{
     public String CustomerName {get; set;}
     public String CustomerNo {get; set;}
     public String OrderStatus {get; set;}
     public String CustomerPONo{get; set;}
     public String Name {get; set;}
     public String GrossAmount {get; set;}
     public String WantedDeliveryDate {get; set;}

}

I make a call out to my webservice and get back my JSON string...all is well.  Now I need to deserialize it which I'm doing like this:
HTTPResponse res = h.send(req);  //response comes back correctly, data is all formatted as I would expect..
       //deserialize the JSON data returned by the web service into the OrderInfo class defined above
        OrderInfo newOrder = new OrderInfo();
        newOrder = (OrderInfo)JSON.deserialize(res.getBody(), OrderInfo.class);
So here is my problem, when I look at the values of my newOrder object they are all null.  Based on examples I viewed, It looked like the deserialize method with a specified object type would update the values in the object.  Do I need to do something else to save the values into the newOrder object?  
    I really appreciate any assistance, I'm sure it's something very simple!  Thanks in advance!!

Best Answer chosen by debiken88
sfdcfoxsfdcfox
It does not update an object; it returns a new object of the particular class (e.g. OrderInfo). You could just as well write the following:

<pre>
OrderInfo newOrder = (OrderInfo)JSON.deserialize(res.getBody(), OrderInfo.class);
</pre>

Most likely, there's a problem with the JSON string itself. By default, the JSON parser ignores blank or missing values, resulting in the values being null. You could try using JSON.deserializeStrict instead, which throws an error if any properties are left unfilled.

I would check the values of res.getBody() and see what it is actually delivering to you, and make sure that the names and values are present and of the correct capitalization (CustomerName is not the same as customerName).

All Answers

sfdcfoxsfdcfox
It does not update an object; it returns a new object of the particular class (e.g. OrderInfo). You could just as well write the following:

<pre>
OrderInfo newOrder = (OrderInfo)JSON.deserialize(res.getBody(), OrderInfo.class);
</pre>

Most likely, there's a problem with the JSON string itself. By default, the JSON parser ignores blank or missing values, resulting in the values being null. You could try using JSON.deserializeStrict instead, which throws an error if any properties are left unfilled.

I would check the values of res.getBody() and see what it is actually delivering to you, and make sure that the names and values are present and of the correct capitalization (CustomerName is not the same as customerName).
This was selected as the best answer
debiken88debiken88
Thank you so much for your reply, using deserializeStrict helped me isolate the issue, although the field names were all correct, one of the fields was a date and it was coming back with escape characters, so when i was expecting "01/15/2014", I was getting "01\/15\/2014", which of course caused errors.   I will have to change the date format so it is recognized as a string when coming back from the web service and handle it on the apex side.  I appreciate you taking the time to help me sort through this!!