Newer Version Available

This content describes an older version of this product. View Latest

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.

Namespace

System

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(Boolean)

Returns a new JSON generator.

Signature

public static System.JSONGenerator createGenerator(Boolean pretty)

Parameters

pretty
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(String)

Returns a new JSON parser.

Signature

public static System.JSONParser createParser(String jsonString)

Parameters

jsonString
Type: String
The JSON content to parse.

Return Value

Type: System.JSONParser

deserialize(String, System.Type)

Deserializes the specified JSON string into an Apex object of the specified type.

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.com API version 24.0 or earlier, this method throws a run-time exception for missing attributes.

Example

The following example deserializes a Decimal value.
1Decimal n = (Decimal)JSON.deserialize(
2               '100.1', Decimal.class);
3System.assertEquals(n, 100.1);

deserializeStrict(String, System.Type)

Deserializes the specified JSON string into an Apex object of the specified type.

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

All attributes in the JSON string must be present in the specified type.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 throws a run-time exception.

Example

The following example deserializes a JSON string into an object of a user-defined type represented by the Car class, which this example also defines.
1swfobject.registerObject("clippy.codeblock-1", "9");public class Car {
2    public String make;
3    public String year;
4}
5
6public void parse() {        
7    Car c = (Car)JSON.deserializeStrict(
8        '{"make":"SFDC","year":"2020"}',
9        Car.class);
10    System.assertEquals(c.make, 'SFDC');
11    System.assertEquals(c.year, '2020');
12}

deserializeUntyped(String)

Deserializes the specified JSON string into collections of primitive data types.

Signature

public static Object deserializeUntyped(String jsonString)

Parameters

jsonString
Type: String
The JSON content to deserialize.

Return Value

Type: Object

Example

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.
1swfobject.registerObject("clippy.exUntyped", "9");String jsonInput = '{\n' +
2    ' "description" :"An appliance",\n' +
3    ' "accessories" : [ "powerCord", ' + 
4      '{ "right":"door handle1", ' + 
5        '"left":"door handle2" } ],\n' +
6    ' "dimensions" : ' + 
7      '{ "height" : 5.5 , ' + 
8        '"width" : 3.0 , ' + 
9        '"depth" : 2.2 },\n' +
10    ' "type" : null,\n' +
11    ' "inventory" : 2000,\n' +
12    ' "price" : 1023.45,\n' +
13    ' "isShipped" : true,\n' +
14    ' "modelNumber" : "123"\n' +
15    '}';
16    
17Map<String, Object> m = 
18   (Map<String, Object>)
19      JSON.deserializeUntyped(jsonInput);
20
21System.assertEquals(
22   'An appliance', m.get('description'));
23        
24List<Object> a = 
25   (List<Object>)m.get('accessories');
26System.assertEquals('powerCord', a[0]);        
27Map<String, Object> a2 = 
28   (Map<String, Object>)a[1];
29System.assertEquals(
30   'door handle1', a2.get('right'));
31System.assertEquals(
32   'door handle2', a2.get('left'));
33
34Map<String, Object> dim = 
35   (Map<String, Object>)m.get('dimensions');
36System.assertEquals(
37   5.5, dim.get('height'));
38System.assertEquals(
39   3.0, dim.get('width'));
40System.assertEquals(
41   2.2, dim.get('depth'));
42        
43System.assertEquals(null, m.get('type'));
44System.assertEquals(
45   2000, m.get('inventory'));
46System.assertEquals(
47   1023.45, m.get('price'));
48System.assertEquals(
49   true, m.get('isShipped'));
50System.assertEquals(
51   '123', m.get('modelNumber'));

serialize(Object)

Serializes Apex objects into JSON content.

Signature

public static String serialize(anyType object)

Parameters

anyType
Type: Object
The Apex object to serialize.

Return Value

Type: String

Example

The following example serializes a new Datetime value.
1Datetime dt = Datetime.newInstance(
2               Date.newInstance(
3                  2011, 3, 22),
4               Time.newInstance(
5                  1, 15, 18, 0)); 
6    String str = JSON.serialize(dt); 
7    System.assertEquals(
8       '"2011-03-22T08:15:18.000Z"',
9       str);

serializePretty(Object)

Serializes Apex objects into JSON content and generates indented content using the pretty-print format.

Signature

public static String serializePretty(Object anyType)

Parameters

anyType
Type: Object
The Apex object to serialize.

Return Value

Type: String