0% found this document useful (0 votes)
75 views4 pages

Working With Json in Apex

The document discusses three JSON methods in Apex: 1. JSON.deserialize() converts JSON to typed Apex values by specifying the expected type. 2. JSON.deserializeStrict() deserializes JSON into a class and throws exceptions if the JSON contains unexpected attributes. 3. JSON.deserializeUntyped() returns an untyped Object that reflects the JSON structure, allowing deserialization of JSON that varies in structure.

Uploaded by

Nidhi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views4 pages

Working With Json in Apex

The document discusses three JSON methods in Apex: 1. JSON.deserialize() converts JSON to typed Apex values by specifying the expected type. 2. JSON.deserializeStrict() deserializes JSON into a class and throws exceptions if the JSON contains unexpected attributes. 3. JSON.deserializeUntyped() returns an untyped Object that reflects the JSON structure, allowing deserialization of JSON that varies in structure.

Uploaded by

Nidhi Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Working with JSON in Apex:

Today we will learn few of the JSON method which we use frequently in
our day-to-day life coding:

1. JSON.deserialize():

The methods JSON.deserialize() convert between JSON and typed


Apex values. When using JSON.deserialize(), you must specify the type
of value you expect the JSON to yield, and Apex will attempt to
deserialize to that type.

This example will deserialize decimal value:

Decimal n = (Decimal)JSON.deserialize(

'100.1', Decimal.class);

System.assertEquals(n, 100.1);

2. JSON. deserializeStrict():

All attributes in the JSON string must be present in the


specified type. If the JSON content contains attributes do 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 deserializes a JSON string into an


object of a user-defined type represented by the Car class,
which this example also defines.

public class Student {


public String Name;

public String Gender;

public void parse() {

Student student = (Student)JSON.deserializeStrict(

'{"Name":"Sweety","Gender":"Female"}',

Student.class);

System.assertEquals(student.Name, Sweety);

System.assertEquals(student.Gender, Female);

3. JSON.deserializeUntyped() :

We have JSON :

Ex1 : {

"Detail": "Accounts",

"data": {

"name": Abc,

"Amount": 40000

}
Ex2 :

"detail": {

"object": "Account",

"ChildRelationship": "Contacts"

},

"data": {

"status": Open

JSON input that varies in this way cannot be handled with strongly
typed Apex classes because its structure is not uniform. The
values for the keys detail and data have different types.

This kind of JSON structure can be deserialized


using JSON.deserializeUntyped(). That method returns an Object,
an untyped value whose actual type at runtime will reflect the
structure of the JSON. In this case, that type would be Map<String,
Object>, because the top level of our JSON is an object. We could
deserialize this JSON via

Map<String, Object> result = (Map<String, Object>)


JSON.deserializeUntyped(jsonString);

You might also like