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

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":1240,"date":"2018-03-05T06:02:34","date_gmt":"2018-03-05T06:02:34","guid":{"rendered":"http:\/\/www.salesforcenextgen.com\/?p=1240"},"modified":"2020-12-28T19:36:44","modified_gmt":"2020-12-28T19:36:44","slug":"how-to-do-json-parsing-of-a-wrapper-class-in-salesforce","status":"publish","type":"post","link":"https:\/\/salesforcenextgen.com\/how-to-do-json-parsing-of-a-wrapper-class-in-salesforce\/","title":{"rendered":"How to do JSON Parsing of a wrapper class in Salesforce"},"content":{"rendered":"

\"JSONHow to do JSON Parsing of a wrapper class in Salesforce.<\/p>\n

What is JSON Parsing?<\/h4>\n

We use JSON parsing to interchange data from a web server.<\/p>\n

Data is always shared as a string from a web server<\/p>\n

To make the data understandable to our system, we do JSON Parsing, and the data becomes a JavaScript object.<\/p>\n

Example of JSON Parsing.<\/p>\n

‘{ “name”:”John”, “age”:30, “city”:”New York”}’<\/p>\n

Use the JavaScript function JSON.parse() to convert text into a JavaScript object<\/p>\n

var\u00a0obj = JSON.parse(‘{ “name”:”John”, “age”:30, “city”:”New York”}’);<\/p>\n

The above example is to do parsing in JavaScript, To do JSON Parsing in Salesforce we have quiet similar approach.<\/p>\n

But first we should understand few terms before we begin the JSON parsing concept.<\/p>\n

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.<\/p>\n

\"JSON<\/p>\n

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.<\/p>\n

To simplify the process of serialization and deserialization, Salesforce has provided a whole class for this purpose, i.e. JSONParser Class.<\/p>\n

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\u00a0field values and get the grand total.<\/p>\n

public class JSONParserUtil {<\/p>\n

@future(callout=true)<\/p>\n

public static void parseJSONResponse() {<\/p>\n

Http httpProtocol = new Http();<\/p>\n

HttpRequest request = new HttpRequest();<\/p>\n

String endpoint = ‘https:\/\/docsample.herokuapp.com\/jsonSample’;<\/p>\n

request.setEndPoint(endpoint);<\/p>\n

request.setMethod(‘GET’);<\/p>\n

HttpResponse response = httpProtocol.send(request);<\/p>\n

System.debug(response.getBody());<\/p>\n

\/\/ Parse JSON response to get all the totalPrice field values.<\/p>\n

JSONParser parser = JSON.createParser(response.getBody());<\/p>\n

Double grandTotal = 0.0;<\/p>\n

while (parser.nextToken() != null) {<\/p>\n

if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&<\/p>\n

(parser.getText() == ‘totalPrice’)) {<\/p>\n

\/\/ Get the value.<\/p>\n

parser.nextToken();<\/p>\n

\/\/ Compute the grand total price for all invoices.<\/p>\n

grandTotal += parser.getDoubleValue();<\/p>\n

}<\/p>\n

}<\/p>\n

system.debug(‘Grand total=’ + grandTotal);<\/p>\n

}<\/p>\n

}<\/p>\n

Example:<\/strong> Parse a JSON String and Deserialize It into Objects, for this example we are taking a hardcoded JSON\u00a0 String and the entire string is parsed into\u00a0Invoice\u00a0objects using the\u00a0readValueAs\u00a0method.<\/p>\n

public static void parseJSONString() {<\/p>\n

String jsonStr =<\/p>\n

‘{“invoiceList”:[‘ +<\/p>\n

‘{“totalPrice”:5.5,”statementDate”:”2011-10-04T16:58:54.858Z”,”lineItems”:[‘ +<\/p>\n

‘{“UnitPrice”:1.0,”Quantity”:5.0,”ProductName”:”Pencil”},’ +<\/p>\n

‘{“UnitPrice”:0.5,”Quantity”:1.0,”ProductName”:”Eraser”}],’ +<\/p>\n

‘”invoiceNumber”:1},’ +<\/p>\n

‘{“totalPrice”:11.5,”statementDate”:”2011-10-04T16:58:54.858Z”,”lineItems”:[‘ +<\/p>\n

‘{“UnitPrice”:6.0,”Quantity”:1.0,”ProductName”:”Notebook”},’ +<\/p>\n

‘{“UnitPrice”:2.5,”Quantity”:1.0,”ProductName”:”Ruler”},’ +<\/p>\n

‘{“UnitPrice”:1.5,”Quantity”:2.0,”ProductName”:”Pen”}],”invoiceNumber”:2}’ +<\/p>\n

‘]}’;<\/p>\n

\/\/ Parse entire JSON response.<\/p>\n

JSONParser parser = JSON.createParser(jsonStr);<\/p>\n

while (parser.nextToken() != null) {<\/p>\n

\/\/ Start at the array of invoices.<\/p>\n

if (parser.getCurrentToken() == JSONToken.START_ARRAY) {<\/p>\n

while (parser.nextToken() != null) {<\/p>\n

\/\/ Advance to the start object marker to<\/p>\n

\/\/\u00a0 find next invoice statement object.<\/p>\n

if (parser.getCurrentToken() == JSONToken.START_OBJECT) {<\/p>\n

\/\/ Read entire invoice object, including its array of line items.<\/p>\n

Invoice inv = (Invoice)parser.readValueAs(Invoice.class);<\/p>\n

system.debug(‘Invoice number: ‘ + inv.invoiceNumber);<\/p>\n

system.debug(‘Size of list items: ‘ + inv.lineItems.size());<\/p>\n

String s = JSON.serialize(inv);<\/p>\n

system.debug(‘Serialized invoice: ‘ + s);<\/p>\n

\/\/ Skip the child start array and start object markers.<\/p>\n

parser.skipChildren();<\/p>\n

}<\/p>\n

}<\/p>\n

}<\/p>\n

}<\/p>\n

}<\/p>\n

\/\/ Inner classes used for serialization by readValuesAs().<\/p>\n

public class Invoice {<\/p>\n

public Double totalPrice;<\/p>\n

public DateTime statementDate;<\/p>\n

public Long invoiceNumber;<\/p>\n

List<LineItem> lineItems;<\/p>\n

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

totalPrice = price;<\/p>\n

statementDate = dt;<\/p>\n

invoiceNumber = invNumber;<\/p>\n

lineItems = liList.clone();<\/p>\n

}<\/p>\n

}<\/p>\n

public class LineItem {<\/p>\n

public Double unitPrice;<\/p>\n

public Double quantity;<\/p>\n

public String productName;<\/p>\n

}<\/p>\n

Another way to do deserialization is as below.<\/p>\n

Invoice inv = (Invoice)JSON.deserialize(jsonStr, Invoice.class);<\/p>\n

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<\/p>\n

List<invoice> lstInvoice = (list<invoice>) JSON.deserialize(jsonStr list<invoice>.class);<\/p>\n

This will parse the string into the respected object list we created in our apex class.<\/p>\n

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

Also, Have a look at the below resources:<\/strong><\/h1>\n
    \n
  1. \n

    Best Salesforce Interview Questions book with Apex and Visualforce concept explained<\/a><\/h3>\n<\/li>\n<\/ol>\n

    Also, Have a look at the below learning resources:<\/strong><\/h1>\n
      \n
    1. \n

      SOQL (Salesforce Object Query Language)<\/a><\/strong><\/em><\/h3>\n<\/li>\n
    2. \n

      Apex Trigger Best Practices and the Trigger Framework<\/a><\/strong><\/em><\/h3>\n<\/li>\n
    3. \n

      Salesforce Interview Question and Answers Part 2<\/a><\/strong><\/em><\/h3>\n<\/li>\n
    4. \n

      Salesforce Interview Questions on Test Class<\/a><\/strong><\/em><\/h3>\n<\/li>\n
    5. \n

      Salesforce-lightning-interview-questions-2018<\/cite><\/span><\/a><\/strong><\/em><\/h3>\n<\/li>\n<\/ol>\n

      \u00a0 \u00a0 \u00a06. Salesforce Interview Questions Batch Class\u00a0<\/a><\/strong><\/em><\/h3>\n

      <\/h3>\n","protected":false},"excerpt":{"rendered":"

      How 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 …
      Continue reading How to do JSON Parsing of a wrapper class in Salesforce<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1243,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[178],"tags":[40,70,73,68,79,66,57,110,76,61,39,45,64,179,38,83,46,111,52],"_links":{"self":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/1240"}],"collection":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/comments?post=1240"}],"version-history":[{"count":4,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/1240\/revisions"}],"predecessor-version":[{"id":2188,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/1240\/revisions\/2188"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/media\/1243"}],"wp:attachment":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/media?parent=1240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/categories?post=1240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/tags?post=1240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}