function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
anil Kumaranil Kumar 

JSON Body in Apex

Hi All,

I would like to prepare dynamic JSON body for all the contact records with masked values like below mentioned format. If contact field value is exist in need to send it as True, if it is not exist send it as False. How to achieve this apex.

{
  "Id":"0032800000240hnAAA",
  "Name": "True",
  "LastName": "True",
  "OtherAddress":"Flase",
  "MailingAddress":"True",
  "Phone":"True",

},
{
  "Id":"0032800000240hnAAA",
  "Name": "False",
  "LastName": "True",
  "OtherAddress":"True",
  "MailingAddress":"True",
  "Phone":"True",

}

Thanks,
Anil Kumar
Raj VakatiRaj Vakati
Hi ,

Here is the sample code, Please modify as per your JSON object.
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('Id', '1234');
gen.writeStringField('Name', 'Welcome');
gen.writeStringField('LastName', 'Hello');
If(con.Phone!=null){
gen.writeStringField('Phone', false);
    
}else{
gen.writeStringField('Phone', true);
    
}


gen.writeEndObject();

String pretty = gen.getAsString().replace('\n', '');
System.debug(pretty);


 
Purushotham YellankiPurushotham Yellanki
Hi Anil, 

There are two ways you can form JSON. One is using JSON Generator Method and other would be using JSON Serializer (for this we creat a Wrapper Class). Above code is perfeclty fine with just one simple change like if you want to use boolean values then its better if we use writeBooleanField (Refere to JSONGenerator Class (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_JsonGenerator.htm)).

List<Contact> lstCnts = [Select Id, Name, LastName, OtherAddress, MailingAddress, Phone From Contact];

Lets say if above is your Contact List, then you can form JSON as below using JSONGenerator

String jsonBody;

if (lstCnts.size()>0) {
    JSONGenerator jsonGen = JSON.createGenerator(true);
    
    for (Contact c: lstCnts) {
        jsonGen.writeStartObject();
        jsonGen.writeStringField('Id', c.Id);
        jsonGen.writeStringField('Name', c.Name);
        jsonGen.writeStringField('LastName', c.LastName);
        jsonGen.writeStringField('MailingAddress', String.ValueOf(c.OtherAddress));
        jsonGen.writeStringField('MailingAddress', String.ValueOf(c.MailingAddress));
        if (c.Phone != null) {
            jsonGen.writeBooleanField('Phone', True);
        } else {
            jsonGen.writeBooleanField('Phone', False);    
        }                    
        jsonGen.writeEndObject();
        jsonBody = jsonGen.getAsString();
        jsonGen.close();
        System.debug('************JSON BODY************' + jsonBody);
    }    
}

Also make sure your fields (OtherAddress, MailingAddress) return some values other wise you will end up hitting "System.NullPointerException: null argument for JSONGenerator.writeStringField()"!