Deprecated: Optional parameter $list declared before required parameter $is_script is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 21

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 45

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 104

Deprecated: Optional parameter $expire declared before required parameter $path is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_functions.php on line 54

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/megamenu.php on line 155

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/mobilemenu.php on line 147

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 583

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 606

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/switch/field_switch.php on line 17

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/switch/field_switch.php on line 17

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/media/field_media.php on line 46

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/media/field_media.php on line 46

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/select/field_select.php on line 17

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/select/field_select.php on line 17

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/editor/field_editor.php on line 46

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/editor/field_editor.php on line 46

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/text/field_text.php on line 17

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/text/field_text.php on line 17

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/slider/field_slider.php on line 40

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/slider/field_slider.php on line 40

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/radio/field_radio.php on line 17

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/radio/field_radio.php on line 17

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/info/field_info.php on line 45

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/info/field_info.php on line 45

Deprecated: Optional parameter $field declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/typography/field_typography.php on line 50

Deprecated: Optional parameter $value declared before required parameter $parent is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/fields/typography/field_typography.php on line 50
How to do JSON Parsing of a wrapper class in Salesforce - Salesforce Next Gen
Title Pic

How to do JSON Parsing of a wrapper class in Salesforce

JSON Parsing SalesforceHow to do JSON Parsing of a wrapper class in Salesforce.

What is JSON Parsing?

We use JSON parsing to interchange data from a web server.

Data is always shared as a string from a web server

To make the data understandable to our system, we do JSON Parsing, and the data becomes a JavaScript object.

Example of JSON Parsing.

‘{ “name”:”John”, “age”:30, “city”:”New York”}’

Use the JavaScript function JSON.parse() to convert text into a JavaScript object

var obj = JSON.parse(‘{ “name”:”John”, “age”:30, “city”:”New York”}’);

The above example is to do parsing in JavaScript, To do JSON Parsing in Salesforce we have quiet similar approach.

But first we should understand few terms before we begin the JSON parsing concept.

To convert an object into a stream of string characters is known as serialization, i. e Object -> String. Once we serialize an object we can transport it over the web server.

JSON Parsing Salesforce

To convert a stream of string into an object is known as deserialization, or simply opposite of serialization is called deserialization, normally it is a response received from a web server. String -> Object.

To simplify the process of serialization and deserialization, Salesforce has provided a whole class for this purpose, i.e. JSONParser Class.

In below a callout was made to web service and it returned a response in JSON format. Then the response is parsed to get all the totalPrice field values and get the grand total.

public class JSONParserUtil {

@future(callout=true)

public static void parseJSONResponse() {

Http httpProtocol = new Http();

HttpRequest request = new HttpRequest();

String endpoint = ‘https://docsample.herokuapp.com/jsonSample’;

request.setEndPoint(endpoint);

request.setMethod(‘GET’);

HttpResponse response = httpProtocol.send(request);

System.debug(response.getBody());

// Parse JSON response to get all the totalPrice field values.

JSONParser parser = JSON.createParser(response.getBody());

Double grandTotal = 0.0;

while (parser.nextToken() != null) {

if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&

(parser.getText() == ‘totalPrice’)) {

// Get the value.

parser.nextToken();

// Compute the grand total price for all invoices.

grandTotal += parser.getDoubleValue();

}

}

system.debug(‘Grand total=’ + grandTotal);

}

}

Example: Parse a JSON String and Deserialize It into Objects, for this example we are taking a hardcoded JSON  String and the entire string is parsed into Invoice objects using the readValueAs method.

public static void parseJSONString() {

String jsonStr =

‘{“invoiceList”:[‘ +

‘{“totalPrice”:5.5,”statementDate”:”2011-10-04T16:58:54.858Z”,”lineItems”:[‘ +

‘{“UnitPrice”:1.0,”Quantity”:5.0,”ProductName”:”Pencil”},’ +

‘{“UnitPrice”:0.5,”Quantity”:1.0,”ProductName”:”Eraser”}],’ +

‘”invoiceNumber”:1},’ +

‘{“totalPrice”:11.5,”statementDate”:”2011-10-04T16:58:54.858Z”,”lineItems”:[‘ +

‘{“UnitPrice”:6.0,”Quantity”:1.0,”ProductName”:”Notebook”},’ +

‘{“UnitPrice”:2.5,”Quantity”:1.0,”ProductName”:”Ruler”},’ +

‘{“UnitPrice”:1.5,”Quantity”:2.0,”ProductName”:”Pen”}],”invoiceNumber”:2}’ +

‘]}’;

// Parse entire JSON response.

JSONParser parser = JSON.createParser(jsonStr);

while (parser.nextToken() != null) {

// Start at the array of invoices.

if (parser.getCurrentToken() == JSONToken.START_ARRAY) {

while (parser.nextToken() != null) {

// Advance to the start object marker to

//  find next invoice statement object.

if (parser.getCurrentToken() == JSONToken.START_OBJECT) {

// Read entire invoice object, including its array of line items.

Invoice inv = (Invoice)parser.readValueAs(Invoice.class);

system.debug(‘Invoice number: ‘ + inv.invoiceNumber);

system.debug(‘Size of list items: ‘ + inv.lineItems.size());

String s = JSON.serialize(inv);

system.debug(‘Serialized invoice: ‘ + s);

// Skip the child start array and start object markers.

parser.skipChildren();

}

}

}

}

}

// Inner classes used for serialization by readValuesAs().

public class Invoice {

public Double totalPrice;

public DateTime statementDate;

public Long invoiceNumber;

List<LineItem> lineItems;

public Invoice(Double price, DateTime dt, Long invNumber, List<LineItem> liList) {

totalPrice = price;

statementDate = dt;

invoiceNumber = invNumber;

lineItems = liList.clone();

}

}

public class LineItem {

public Double unitPrice;

public Double quantity;

public String productName;

}

Another way to do deserialization is as below.

Invoice inv = (Invoice)JSON.deserialize(jsonStr, Invoice.class);

This will convert a the response string into a single Invoice object. But what if we want to deserialise a list of Invoice object. In this case we will use the below code to deserialise the list

List<invoice> lstInvoice = (list<invoice>) JSON.deserialize(jsonStr list<invoice>.class);

This will parse the string into the respected object list we created in our apex class.

This approach seems easy but it is always better to use the JSONParser Class to serialize and deserialize the object/object list

Also, Have a look at the below resources:

  1. Best Salesforce Interview Questions book with Apex and Visualforce concept explained

Also, Have a look at the below learning resources:

  1. SOQL (Salesforce Object Query Language)

  2. Apex Trigger Best Practices and the Trigger Framework

  3. Salesforce Interview Question and Answers Part 2

  4. Salesforce Interview Questions on Test Class

  5. Salesforce-lightning-interview-questions-2018

     6. Salesforce Interview Questions Batch Class 

Sumit Datta

Sumit Datta

I am a 5x Certified Salesforce developer with overall 7 years of IT experience and 5 years of Implementation experience in Salesforce. I am here to share my knowledge and help Beginners in Salesforce to understand the concepts of Apex, Visualforce, Salesforce Lightning and Salesforce Configuration.

1 Comment

  1. Salesforce Learner March 5, 2018 Reply

    Nice article

Leave a Comment

Your email address will not be published.