Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

It seems that json.serialize method converts null to String 'null'

Contact con = null;
String jsonCon = JSON.serialize(con);

Results in debug:

15:21:35.039 (39195000)|USER_DEBUG|[1]|DEBUG|true
15:21:35.039 (39503000)|USER_DEBUG|[6]|DEBUG|null
15:21:35.039 (39551000)|USER_DEBUG|[7]|DEBUG|false
share|improve this question

closed as not a real question by eyescream, Mike Chale, Ryan Guest, Daniel Blackhall, Saariko Dec 10 '12 at 7:29

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

OK, but what's your question? Are you looking for advice on how to handle this or just confirmation that this is expected behavior? – Mike Chale Dec 8 '12 at 2:09
I except it working as described in that function String.isEmpty() "....Returns true if the specified String is empty ('') or null; otherwise, returns false...." – user1019 Dec 8 '12 at 20:10
@grigriforce it looks to me like it's properly returning null, and not the string 'null'. – Mikey Dec 8 '12 at 21:36
@Mikey It does this correctly for everything but strings. – Greg Grinberg Dec 9 '12 at 18:16
@grigriforce very interesting... – Mikey Dec 9 '12 at 19:54

As per the json spec, null is represented by the keyword null, so this seems right.

share|improve this answer
but it seems like the json.serialize is returning 'null' rather than null for strings. – Greg Grinberg Dec 10 '12 at 5:08
that is correct the value null should generate the keyword null. (think about the case where you have a map with a key foo and a value null, you'd get { "foo" : null } – superfell Dec 10 '12 at 5:41 – superfell Dec 10 '12 at 5:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.