Parsing JSON the easy way using Apex

JSON is a lightweight data-interchange format. It is the most preferred way of transferring data over web.

In Salesforce, this comes to most use when integrating with an external system.

In most of the integrations, you typically use HTTP Callouts to the end points defined by various services, and the most common response format returned by these is JSON.

Now, parsing this JSON would be very time consuming if not done in the right way. One way of doing this would be manually parsing the complete JSON using the JSONParser method.

But the easiest way of Parsing JSON would be to de-serialize it into an Object.
 <Class/Object> n = (<Class/Object>)JSON.deserialize('JSON', <Class/Object>.class);
This one statement would completely de-serialize the JSON and store the values in the respective member variables of the class..

For this, first we need to create a class or an Inner class, which would contain all the variables that are to be stored from the JSON.

The JSON2Apex is a very handy tool for this. Goto http://json2apex.herokuapp.com/ and paste the JSON String in the space provided, and click on create Apex.

This would give you a class with member variables from the JSON String. You can just use this class as is or you can paste it into your existing class like an inner class.

You then use the JSON.de-serialize to parse the JSON.

Below is the sample output from a callout to the Tooling API of Salesforce to query the Test Coverage (Read about Export Code Coverage to Excel)
{"size":141,"totalSize":141,"done":true,"queryLocator":null,"entityTypeName":"ApexCodeCoverageAggregate","records":[{"attributes":{"type":"ApexCodeCoverageAggregate","url":"/services/data/v33.0/tooling/sobjects/ApexCodeCoverageAggregate/715000000LxWDAA0"},"Id":"715000000LxWDAA0","ApexClassOrTrigger":{"attributes":{"type":"Name","url":"/services/data/v33.0/tooling/sobjects/ApexClass/01p000000C8J2AAK"},"Name":"MyCustomClass1"},"NumLinesCovered":0,"NumLinesUncovered":10},{"attributes":{"type":"ApexCodeCoverageAggregate","url":"/services/data/v33.0/tooling/sobjects/ApexCodeCoverageAggregate/715e0000000LxWEAA0"},"Id":"710000000LxWEAA0","ApexClassOrTrigger":{"attributes":{"type":"Name","url":"/services/data/v33.0/tooling/sobjects/ApexClass/01pe0000000C8JCAA0"},"Name":"MyCustomClass2"},"NumLinesCovered":7,"NumLinesUncovered":5}]}
This JSON would produce the below class when run through JSON2Apex Heroku App (http://json2apex.herokuapp.com/)
public class JSON2Apex {

	public class Attributes {
		public String type;
		public String url;
	}

	public class Records {
		public Attributes attributes;
		public String Id;
		public ApexClassOrTrigger ApexClassOrTrigger;
		public Integer NumLinesCovered;
		public Integer NumLinesUncovered;
	}

	public class ApexClassOrTrigger {
		public Attributes attributes;
		public String Name;
	}

	public Integer size;
	public Integer totalSize;
	public Boolean done;
	public Object queryLocator;
	public String entityTypeName;
	public List records;

	
	public static JSON2Apex parse(String json) {
		return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
	}
}
You can use this class, and deserialize the JSON by
JSON2Apex myClass = JSON2Apex.parse(jsonString);
and you can then access the variables of this class by
myClass.done, myClass.size, myClass.records
Note that separate inner classes are generated for each complex data type of the parsed JSON.
For Example :
myClass.records[0].ApexClassorTrigger.Name
Here, ApexClassorTrigger is an inner class, with 2 variables : Name and Attributes which in turn has 2 variables Type and URL.

posted on 3 April 2015

JSON Deserialize Apex Callouts Inner Class Wrappers