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
Sammy ShkSammy Shk 

cannot start an object, expecting a field Name

I'm trying to generate Json in following format
{
   "name":"TestAPIFromTrigger",
   "discovered":null,
   "occurred_on":null,
   "group":{
      "id":7387,
      "name":"Default Group"
   },
   "description":"",
   "channel":{
      "source":"API"
   },
   "submitted_by":{
      "given_name":"First Name",
      "surname":"Last Name",
      "email":"SSS@edu",
      "phone":"78965555"
   }

Apex code

Patient_Satisfaction__c c = [select id, Name, Reporter_Phone__c, Description_of_Feedback__c from Patient_Satisfaction__c where Patient_Relation__c ='Referred to Privacy Office' order by lastmodifiedDate desc limit 1];
    JSONGenerator gen = JSON.createGenerator(true);
    gen.writeStartObject();
    gen.writeObjectField('Name',c.Name);
    gen.writeObjectField('incident_group_id',7387);
    gen.writeObjectField('description',c.Description_of_Feedback__c);
    gen.writeStartObject();
    gen.writeFieldName('submitted_by');
    gen.writeStartArray();
    gen.writeObjectField('phone',c.Reporter_Phone__c);
    gen.writeEndArray();
    gen.writeEndObject();
    String jsonS = gen.getAsString(); 
    System.debug('jsonMaterials'+jsonS)
sachinarorasfsachinarorasf
Hi Sammy,

Please go through the below link.

https://salesforce.stackexchange.com/questions/181545/jsonerror-system-jsonexception-can-not-start-an-object-expecting-field-name

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com