Skip to main content

JSON in salesforce apex

 JSONGenerator Class some frequently used Methods:

Method             Description

getAsString       Returns the generated JSON content, and also this method closes the JSON generator if it isn’t closed already.

writeBooleanField Writes a field name and value pair using the specified field name and Boolean value.

writeDateField    Writes a field name and value pair using the specified field name and date value. The date value is written in the ISO-8601 format.

writeEndArray     Writes the ending marker of a JSON array (‘]’).

writeEndObject    Writes the ending marker of a JSON object (‘}’).

writeNullField    Writes a field name and value pair using the specified field name and the JSON null literal value.

writeNumberField  Writes a field name and value pair using the specified field name and decimal, double, integer, long value.

writeStartArray   Writes the starting marker of a JSON array (‘[‘).

writeStartObject  Writes the starting marker of a JSON object (‘{‘).

writeStringField  Writes a field name and value pair using the specified field name and string value.



{

"ContactList" : [ {

    "ID" : "0032v00002j3qBCAAY",

    "FirstName" : "Bhole",

    "LastName" : "Shankar",

    }, {

    "ID" : "0032v00002j3qBDAAY",

    "FirstName" : "Brajesh",

    "LastName" : "Jha",

    } ]

}



public class JSONContacts {

    public static void JSONContactsMethod (){

        List<Contact> conList = [SELECT ID, firstName, lastName FROM Contact];

        if(conList.size()>0){

            JSONGenerator jsonGen = JSON.createGenerator(true);

            jsonGen.writeStartObject();

            jsonGen.writeFieldName('ContactList');

            jsonGen.writeStartArray();

            for(Contact con : conList){

                jsonGen.writeStartObject();

                jsonGen.writeStringField('ID',con.Id);

                jsonGen.writeStringField('FirstName',con.firstName);

                jsonGen.writeStringField('LastName',con.lastName);

                jsonGen.writeEndObject();

            }

            jsonGen.writeEndArray();

            jsonGen.writeEndObject();

            String jsonData = jsonGen.getAsString();

            System.debug('Json Data = ' + jsonData);

        }

        

    }

}

Comments

  1. How do you handle null values? This will break very fast in the real world.

    ReplyDelete
    Replies
    1. writeNull Writes the JSON null literal value
      writeNullField Writes a field name and value pair using the specified field name and
      the JSON null literal value.

      Delete

Post a Comment

Popular posts from this blog

COPADO QUESTION BANK

1. Dave has committed a profile and a custom object on a user story. However, while reviewing the User Story selections grid, he can only see the profile there. What could be the reason for this? ***He has committed the custom object as Retrieve Only since the object already exists in the destination environment. He has clicked on Delete Git Selections in the User Story selections grid and the custom object has been deleted. The User Story selections grid has not been refreshed. wrong None are correct. 2. What is an org credential?4 A connection between a specific user and a Git branch. A Salesforce environment. A connection to a Git repository. A connection between a specific user and one Salesforce environment. 3. What are some of the challenges in the development lifecycle Copado helps address?4 Limited set of quality checks. Lack of monitoring and compliance. The struggle of migrating data from multiple objects. All are correct. 4. When committing changes...