You got back a JSON string either as a response from a REST service, passed through from your visual force page via remote action or passed from your lighting component to your apex controller, now how do you parse this information? For simple JSON structures Salesforce can automatically parse them for you. There are tools online that would generate apex classes from the JSON string that would assist in parsing the JSON input.

It is good to get back to the basics and understand a little bit how the JSON string is structured and how the parsing is done.

What is JSON? JSON is the acronym of Javascript Object Notation and is a lightweight format that is used for data exchange between servers and modern applications. It is based on a subset of JavaScript language (the way objects are built in JavaScript).

JSON structure comes in the following formats:

collection of name values based on the following data types

string

{"name" : "olopsman"}

number

{"age" : 42}

object (JSON object)

{"car" : {"model" : "outlander", "year" : "2016"}}

array

{"kids" : ["penny", "padma", "amber", "pauline"]}

boolean

{“married” : true}

null

{"entrepreneur" : null}

array of values or objects

["web development","UX design","salesforce"]

[{"employment": "davanti"},{"employment":"cloud concept"}]

Having a good grasp of the JSON structure is key on knowing which Salesforce JSON class methods to use for parsing.

JSON.deserialize(jsonString, apexType)
JSON.deserialize(jsonString, apexType) – the apexType could be a custom object, SObject or a user defined class. With API version 35 and higher when JSON content has attributes not found in the custom apex class or fields on a custom object or Sobject, deserialization it ignores those attributes and parse the JSON

Deserialized into standard object sample – only the name attribute can me mapped to the Account object, the rest are ignored.

Run and execute as anonymous apex

Deserialized using an Apex class sample: – json attributes not defined in the Apex class will be ignored.

Save this class

Run and execute as anonymous apex

Slight variation, JSON string is an array of object. Run and execute as anonymous apex

JSON.deserializeStrict(jsonString, apexType)
JSON.deserializeStrict(jsonString, apexType) – similar to above but the deserialiazation fails if those attributes are not found either as fields on the custom object or Sobject, or not defined in the custom apex class.

Run and execute as anonymous apex

will throw this error

JSON.createParser(jsonString)
createParser(jsonString) – this would return the JSONParser class. This is useful for retrieving specific data(eg. access_token from oAuth response) without a need of an Apex class or if you want to parse the JSON to control how it maps to an Apex class.

Here is slight variation of our example, an array of objects and one of the attributes is not a supported variable naming convention in Apex.

Update the Apex class to the following:

Of course you can always have other alternative solutions and options like the two below.

You can use the deserializeUntyped or deserialize with the Apex class and ignore the _id attribute if you do not need it on your application.

Run a String replace method on the JSON string to change the _id attribute to x_id, then use the deserialize method with the Apex class.

Or use the JSON.createParser to create a JSONParser object where you manually traverse the JSON token and values. Honestly using the parser is not that intuitive, I recommend at this point to look into online tools such as JSON2Apex for bit complex JSON structures. Kudos to the people behind this tool for making it available to us devs.

For the sake of this tutorial, let’s try using the JSONParser and the JSONToken Enum to parse this input.

Run the following on anonymous apex

So what I did was using while statements I looped through the array of objects and traversed each token and mapped the attributes to the Apex Class.

Here is the final apex class.

Call it from anonymous apex like so:

Phew! That was a bit to cover but investing the time to learn how the JSON parsing is worth it.

Sample codes used in the tutorial can be found in my github repo here. I’ll post a video tutorial soon.