How to Parse JSON Response in Apex Salesforce

by Rijwan Mohmmed

Hello friends, Today we are going to discuss How to Parse JSON Response in Apex Salesforce. Most of the JSON data we get when we do REST API Callout in Apex Class. But we face an issue that apex can’t read JSON data. So we do convert our JSON data in Apex class format. So we can read this easily.

Also check this: Get current page parameters in LWC

Highlights Points :

  1. We use JSON2Apex Converter.
  2. Tool convert the JSON data in Apex Class.
  3. We can use this generated class as a Wrapper class or seperated class.

Process :

Step1: First we get JSON data from REST API. Or if you already have then you can you this.

Step2: Go to JSON2Apex Converter and paste the JSON data.


Step3: Create this apex class in Org and Use for parse the data.

Code :

    "status": "OK",
    "message": "",
    "zones": [
            "countryCode": "CI",
            "countryName": "C\u00f4te d'Ivoire",
            "zoneName": "Africa\/Abidjan",
            "gmtOffset": 0,
            "timestamp": 1642527697
            "countryCode": "GH",
            "countryName": "Ghana",
            "zoneName": "Africa\/Accra",
            "gmtOffset": 0,
            "timestamp": 1642527697
            "countryCode": "ET",
            "countryName": "Ethiopia",
            "zoneName": "Africa\/Addis_Ababa",
            "gmtOffset": 10800,
            "timestamp": 1642538497
            "countryCode": "DZ",
            "countryName": "Algeria",
            "zoneName": "Africa\/Algiers",
            "gmtOffset": 3600,
            "timestamp": 1642531297


// Generated by JSON2Apex

public class JSON2Apex {

	public String status;
	public String message;
	public List<Zones> zones;

	public class Zones {
		public String countryCode;
		public String countryName;
		public String zoneName;
		public Integer gmtOffset;
		public Integer timestamp;

	public static JSON2Apex parse(String json) {
		return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);

GetZoneData.cls :

public class GetZoneData {

    public List<SelectOption> getItems(){
        List<SelectOption> options = new List<SelectOption>();
        Http ht =new Http();
        HttpRequest req = new HttpRequest();
        HttpResponse res = ht.send(req);
        JSON2Apex obj = JSON2Apex.parse(res.getBody());
        System.debug('message => ' +obj.message);
        System.debug('status => ' +obj.status);

        for(JSON2Apex.Zones zone : obj.zones){
           options.add(new SelectOption(String.valueOf(zone.gmtOffset),zone.countryName));
       return options;

Output :


You may also like


Cameron January 2, 2023 - 9:01 pm

does not working, it throws an error “variable does not exist: obj”

Rijwan Mohmmed January 3, 2023 - 5:33 am

HI @CAMERON , Please try this : JSON2Apex obj. I also update the code. Thanks for find bug in my code.

Mr Jeyasingh John Samuel March 24, 2023 - 2:26 pm

How to make the response fields dynamic? i.e. If I want to add a new field say “currency__c” in the Zone, then how do I do it without any change in apex code/dto?


Leave a Comment