How to write JSON in salesforce apex with simple example


In this blog we are going to write simple JSON program "JSONContacts". We simple query the 5 contacts in List and convert it into JSON structure. Before that we will look into the the terms and syntax which will be used to create JSON.

JSON structure comes in the following formats:

collection of name values based on the following data types
string {"name" : "olopsman"}

number {"age" : 42}

object (JSON object) {"car" : {"model" : "outlander", "year" : "2016"}}

array {"kids" : ["penny", "padma", "amber", "pauline"]}

boolean {“married” : true}

null {"entrepreneur" : null}

array of values or objects 
["web development","UX design","salesforce"]
[{"employment": "davanti"},{"employment":"cloud concept"}]


We can generate standard JSON-encoded content, using the JSONGenerator class methods.

JSONGenerator Class Methods:

Method Description
close Closes the JSON generator.No more content can be written after the JSON generator is closed.

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

isClosed Returns true if the JSON generator is closed; otherwise, returns false.

writeBlob Writes the specified Blob value as a base64-encoded string.

writeBlobField Writes a field name and value pair using the specified field name and BLOB value.

writeBoolean Writes the specified Boolean value.

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

writeDate Writes the specified date value in the ISO-8601 format.

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.

writeDateTime Writes the specified date and time value in the ISO-8601 format.

writeDateTimeField Writes a field name and value pair using the specified field name and date and time value. The date and time 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 (‘}’).

writeFieldName Writes a field name.

writeId Writes the specified ID value.

writeIdField         Writes a field name and value pair using the specified field name and identifier value.

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.

writeNumber Writes the specified decimal, double, integer and long value.

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

writeObject Writes the specified Apex object in JSON format

writeObjectField  Writes a field name and value pair using the specified field name and Apex object.

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

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

writeString Writes the specified string value.

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

writeTime Writes the specified time value in the ISO-8601 format.

writeTimeField Writes a field name and value pair using the specified field name and time value in the ISO-8601 format.

Program


To run the above program open anonymous window in developer console and just execute it. you will see the output in debug log as below.

"ContactList" : [ {
    "ID" : "0032v00002j3qBCAAY",
    "FirstName" : "Andy",
    "LastName" : "Young",
    "Phone" : "(785) 241-6200"
  }, {
    "ID" : "0032v00002j3qBDAAY",
    "FirstName" : "Tim",
    "LastName" : "Barr",
    "Phone" : "(312) 596-1000"
  }, {
    "ID" : "0032v00002j3qBEAAY",
    "FirstName" : "John",
    "LastName" : "Bond",
    "Phone" : "(312) 596-1000"
  }, {
    "ID" : "0032v00002j3qBFAAY",
    "FirstName" : "Stella",
    "LastName" : "Pavlova",
    "Phone" : "(212) 842-5500"
  }, {
    "ID" : "0032v00002j3qBGAAY",
    "FirstName" : "Lauren",
    "LastName" : "Boyle",
    "Phone" : "(212) 842-5500"
  } ]
}
Reactions

Post a Comment

0 Comments