Take the 2-minute tour ×
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.

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

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

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.

3  
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
1  
@grigriforce it looks to me like it's properly returning null, and not the string 'null'. gist.github.com/4242074 –  Mikey Dec 8 '12 at 21:36
1  
@Mikey It does this correctly for everything but strings. gist.github.com/4246349 –  Greg Grinberg Dec 9 '12 at 18:16
1  
@grigriforce very interesting... –  Mikey Dec 9 '12 at 19:54
show 1 more comment

1 Answer 1

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. gist.github.com/4246349 –  Greg Grinberg Dec 10 '12 at 5:08
1  
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
    
gist.github.com/4248650 –  superfell Dec 10 '12 at 5:45
add comment

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