No Results
Search Tips:
- Please consider misspellings
- Try different search keywords
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(Boolean)
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)
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)
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
1Decimal n = (Decimal)JSON.deserialize(
2 '100.1', Decimal.class);
3System.assertEquals(n, 100.1);deserializeStrict(String, System.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
Example
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)
Signature
public static Object deserializeUntyped(String jsonString)
Parameters
- jsonString
- Type: String
- The JSON content to deserialize.
Return Value
Type: Object
Example
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)
Signature
public static String serialize(anyType object)
Parameters
- anyType
- Type: Object
- The Apex object to serialize.
Return Value
Type: String
Example
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)
Signature
public static String serializePretty(Object anyType)
Parameters
- anyType
- Type: Object
- The Apex object to serialize.
Return Value
Type: String