1

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
6
  • 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, 2012 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, 2012 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, 2012 at 21:36
  • 1
    @Mikey It does this correctly for everything but strings. gist.github.com/4246349 Dec 9, 2012 at 18:16
  • 1
    @grigriforce very interesting...
    – Mikey
    Dec 9, 2012 at 19:54

1 Answer 1

11

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

3
  • but it seems like the json.serialize is returning 'null' rather than null for strings. gist.github.com/4246349 Dec 10, 2012 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, 2012 at 5:41
  • gist.github.com/4248650
    – superfell
    Dec 10, 2012 at 5:45

Not the answer you're looking for? Browse other questions tagged .