Newer Version Available
JSON Class
Namespace
Usage
Use the methods in the System.JSON class to perform round-trip JSON serialization and deserialization of Apex objects.
JSON Methods
The following are methods for JSON. All methods are static.
createGenerator(prettyPrint)
Signature
public static System.JSONGenerator createGenerator(Boolean prettyPrint)
Parameters
- prettyPrint
- Type: Boolean
- Determines whether the JSON generator creates JSON content in pretty-print format with the content indented. Set to true to create indented content.
Return Value
Type: System.JSONGenerator
createParser(jsonString)
Signature
public static System.JSONParser createParser(String jsonString)
Parameters
- jsonString
- Type: String
- The JSON content to parse.
Return Value
Type: System.JSONParser
deserialize(jsonString, apexType)
Signature
public static Object deserialize(String jsonString, System.Type apexType)
Parameters
- 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.
Return Value
Type: Object
Usage
If the JSON content to parse contains attributes not present in the Apex type specified in the argument, such as a missing field or object, this method ignores these attributes and parses the rest of the JSON content. However, for Apex saved using Salesforce API version 24.0 or earlier, this method throws a run-time exception for missing attributes.
Example
deserializeStrict(jsonString, apexType)
Signature
public static Object deserializeStrict(String jsonString, System.Type apexType)
Parameters
- 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.
Return Value
Type: Object
Usage
Example
deserializeUntyped(jsonString)
Signature
public static Object deserializeUntyped(String jsonString)
Parameters
- jsonString
- Type: String
- The JSON content to deserialize.
Return Value
Type: Object
serialize(objectToSerialize)
Signature
public static String serialize(Object objectToSerialize)
Parameters
- objectToSerialize
- Type: Object
- The Apex object to serialize.
Return Value
Type: String
Example
serializePretty(objectToSerialize)
Signature
public static String serializePretty(Object objectToSerialize)
Parameters
- objectToSerialize
- Type: Object
- The Apex object to serialize.
Return Value
Type: String