18

I have been using JSON.Serialize method extensively in one of customer's project. Today salesforce has started rolling out Spring '13 release on sandboxes. My customer's sandbox( cs11) is not listed on https://trust.salesforce.com/trust/maintenance/ but still Spring '13 is available in this sandbox.

I have noticed a change that if I serialize a sObject, Json Serializer doesn't return fields with null value in json. Before Spring '13 release it was working fine.

If this was a needed change to reduce size of serialized json, Salesforce should have made this version specific change or a overloaded method to keep backward compatibility. It has broken 30%-40% of my code that I will need to rewrite.

Salesforce please undo this change.

Current output: [ { "attributes": { "type": "ABC_c", "url": "/services/data/v27.0/sobjects/ABC_c/xxxx" }, "Active__c": false, "Id": "XXXX", "Name": "ABC" }, ]

Old output: [ { "attributes": { "type": "ABC_c", "url": "/services/data/v27.0/sobjects/ABC_c/xxxx" }, "Active_c": false, "FirstName_c": null, "Id": "XXXX", "Name": "ABC" }, ]

Ishan Kumar
  • 181
  • 1
  • 3
  • 1
    Can you just make the class deprecated to version 26.0 and try.I just verified and yes JSON.serialise() no more returns null values – Mohith Shrivastava Jan 12 '13 at 14:59
  • 1
    Also only one way i can think of now is if the value is null you may need to explicitly say value=null to get back the same in JSON Account acc=[Select Id,name,Name_Suffix__c from Account where id='001W0000007Ky0Z']; if(acc.Name_Suffix__c!=null) {acc.Name_Suffix__c=null; }System.debug('JSON STRING'+acc); – Mohith Shrivastava Jan 12 '13 at 16:07
  • 3
    As Mohith says, all changes can be version specific - you should be able to mark the class as API 26.0 and have nulls encoded. – metadaddy Jan 12 '13 at 17:44
  • Do you have some code to reproduce this? I can't find this change in the release notes and I just tested on a spring '13 sandbox and got the same results as before. – Greg Grinberg Jan 14 '13 at 01:27
  • I'm having the same issue but I'm unable to get my code to use version 27 (or I'm doing something completely wrong). I've tried to change the Version Settings for all of the classes used on the page and the (visual force) page to use 27.0 (I even tried 26.0) to no avail. The JSON being returned still references v28.0. Here's a snippet in case I'm completely missing something... List result = Database.query(soql); String JSONString = JSON.serialize(result); System.debug('result[0].Catalog_UOM__c ' + result[0].Catalog_UOM__c); // this shows a null value System.debug('JSONString : ' + JSO – Bob Jul 09 '13 at 16:52
  • Salesforce team released a fix for the issue for every one on -feb 6 after logging a case. Now with version 27 following returns null fields also [{"attributes": {"type":"Account","url":"/services/data/v27.0/sobjects/Account/001A000000x9TpiIAE"}, "Id":"001A000000x9TpiIAE","Name":"Spring13","Phone":null}] – Ugesh Gali Feb 14 '13 at 06:37
  • I am using version 25.0 on my classes still it is not returning null fields in JSON. Can someone from Salesforce Engineering team confirm this issue and expected behavior of this during final roll-out on production instances. –  Jan 13 '13 at 05:18
  • Stackexchange note; it seems that you have created two accounts on salesforce.stackexchange.com, it would be best to use one. As an answer to your question, to get input from the Salesforce team, I think it would be best to log a case. – user254875486 Jan 13 '13 at 10:25
  • +1 on both of @Lex's suggestions. Salesforce.stackexchange.com is Q&A community - please log a case. Setting the version to 25.0 should result in the 'old' behavior! – metadaddy Jan 13 '13 at 18:38
  • Were you able to fix this problem? We are also getting the same problem. If we tried to change the API version of web service, it doesn't reflect while calling web service. Due to that we need to change our code and put the condition to handle no fields. Which will take much processing on mobile device. Because we are calling this web service from mobile device. Thanks and Regards. – Ashish Jul 02 '14 at 11:45

1 Answers1

10

The Apex development team changed the behavior of the JSON serialization in response to several customer issues.

As you are aware, we will typically version changes to the system behavior where possible, in order to protect backward compatibility. There are situations in which we do not version behavior, however. We made an assumption that JSON parsers would not have an issue not receiving null values, since the information received is not relevant. It's like the junk mail you reflexively throw out when sifting through your mail.

As per your post, it's clear that some parsing strategies depend on null values being included in the JSON output. We apologize that our assumption was incorrect, and that your integration and others were unable to operate with the new version.

We have undone the change for the Spring '13 release. To support the original customer cases that prompted these changes, we will version in the originally intended fix in the Summer release.

Thanks,

Josh Kaplan

Product Manager, Apex Code

Josh Kaplan
  • 752
  • 6
  • 8
  • 1
    Was this added? still not able to get null values with version 28? Am I missing something? – Chirag Mehta Aug 05 '13 at 06:31
  • I think it takes null values now – SEuser Jan 06 '15 at 09:41
  • I can't get any null values. Tried all version from 32 down to 25. No luck. – Uwe Heim Feb 12 '15 at 17:45
  • Im still seeing JSON.serialize strip out null parameters even in the latest release? Any insight into a way around this? – Keith Mancuso Mar 12 '15 at 04:45
  • 1
    @Josh Kaplan why can't you allow the developer to decide the functionality?! This could easily just be an annotation on the class or a parameter on the method. Apex serialization is so half baked it hardly even usable. And not being able to set the serialization property name different from the APEX property is a HUGE issue. – NSjonas Aug 09 '15 at 15:58
  • for example, I'm integrating with the JIRA api. If I want to update only a couple fields on an object in jira, and apex sets every field to null, then it is going to set all my fields to null in jira! – NSjonas Aug 09 '15 at 16:00