JSON Class
Contains methods for serializing Apex objects into JSON format and deserializing JSON content that was serialized using the serialize method in this class.
Use the methods in the System.JSON class to perform round-trip JSON serialization and deserialization of Apex objects.
See Also
The following are methods for JSON. All methods are static.
-
Returns a new JSON generator.
-
Returns a new JSON parser.
-
deserialize(jsonString, apexType)
Deserializes the specified JSON string into an Apex object of the specified type.
-
deserializeStrict(jsonString, apexType)
Deserializes the specified JSON string into an Apex object of the specified type.
-
deserializeUntyped(jsonString)
Deserializes the specified JSON string into collections of primitive data types.
-
Serializes Apex objects into JSON content.
-
serialize(objectToSerialize, suppressApexObjectNulls)
Suppresses
nullvalues when serializing Apex objects into JSON content. -
serializePretty(objectToSerialize)
Serializes Apex objects into JSON content and generates indented content using the pretty-print format.
-
serializePretty(objectToSerialize, suppressApexObjectNulls)
Suppresses
nullvalues when serializing Apex objects into JSON content and generates indented content using the pretty-print format.
Returns a new JSON generator.
public static System.JSONGenerator createGenerator(Boolean prettyPrint)
- prettyPrint: Type: Boolean Determines whether the JSON generator creates JSON content in pretty-print format with the content indented. Set to
trueto create indented content.
Type: System.JSONGenerator
Returns a new JSON parser.
public static System.JSONParser createParser(String jsonString)
- jsonString: Type: String The JSON content to parse.
Type: System.JSONParser
Deserializes the specified JSON string into an Apex object of the specified type.
public static Object deserialize(String jsonString, System.Type apexType)
- jsonString: Type: String The JSON content to deserialize.
- apexType: Type: System.Type The Apex type of the object that this method creates after deserializing the JSON content.
Type: Object
If the JSON content contains attributes not present in the System.Type argument, such as a missing field or object, deserialization fails in some circumstances. When deserializing JSON content into a custom object or an sObject using Salesforce API version 34.0 or earlier, this method throws a runtime exception when passed extraneous attributes. When deserializing JSON content into an Apex class in any API version, or into an object in API version 35.0 or later, no exception is thrown. When no exception is thrown, this method ignores extraneous attributes and parses the rest of the JSON content.
The following example deserializes a Decimal value.
Deserializes the specified JSON string into an Apex object of the specified type.
public static Object deserializeStrict(String jsonString, System.Type apexType)
- jsonString: Type: String The JSON content to deserialize.
- apexType: Type: System.Type The Apex type of the object that this method creates after deserializing the JSON content.
Type: Object
All attributes in the JSON string must be present in the specified type. If the JSON content contains attributes not present in the System.Type argument, such as a missing field or object, deserialization fails in some circumstances. When deserializing JSON content with extraneous attributes into an Apex class, this method throws an exception in all API versions. However, no exception is thrown when you use this method to deserialize JSON content into a custom object or an sObject.
The following example defines the Car class, a user-defined type.
The parse method in this class deserializes a JSON string into a Car object and verifies the deserialized values.
Deserializes the specified JSON string into collections of primitive data types.
public static Object deserializeUntyped(String jsonString)
- jsonString: Type: String The JSON content to deserialize.
Type: Object
The following example deserializes a JSON representation of an appliance object into a map that contains primitive data types and further collections of primitive types. It then verifies the deserialized values.
Serializes Apex objects into JSON content.
public static String serialize(Object objectToSerialize)
- objectToSerialize: Type: Object The Apex object to serialize.
Type: String
The following example serializes a new Datetime value.
Suppresses null values when serializing Apex objects into JSON content.
public static String serialize(Object objectToSerialize, Boolean suppressApexObjectNulls)
-
objectToSerialize: Type: Object The Apex object to serialize.
-
suppressApexObjectNulls: Type: Boolean If true, remove
nullvalues before serializing the JSON object.This parameter doesn’t apply to sObjects retrieved via SOQL.
Type: String
This method allows you to specify whether to suppress null values when serializing Apex objects into JSON content.
Serializes Apex objects into JSON content and generates indented content using the pretty-print format.
public static String serializePretty(Object objectToSerialize)
- objectToSerialize: Type: Object The Apex object to serialize.
Type: String
Suppresses null values when serializing Apex objects into JSON content and generates indented content using the pretty-print format.
public static String serializePretty(Object objectToSerialize, Boolean suppressApexObjectNulls)
-
objectToSerialize: Type: Object The Apex object to serialize.
-
suppressApexObjectNulls: Type: Boolean If true, remove
nullvalues before serializing the JSON object.This parameter doesn’t apply to sObjects retrieved via SOQL.
Type: String