• brian49
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies

I'm trying to write out JSON from multiple Salesforce objects using the JSONGenerator class, with the below lines as a small example. 

 JSONGenerator gen = JSON.createGenerator(true);   
 gen.writeStartObject(); 
 gen.writeStringField('companyname', account.name); 

 gen.writeStringField('permalink', opp.permalink__c); 

gen.writeEndObject()

 

When writing the string fields, I can't guarantee the string won't be null (for example, that opp.permalink__c is null), which means that the generator throws an error when I try when I attempt to write a null string. To get around it, I could do a null check on every field, and set it to the empty string instead of null, but I'm retrieving a lot of fields and don't want to make the code too unreadable... Is there a cleaner fix for this?

 

Thanks in advance!