function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PCPC 

Get particular json element value using apex

Below is the json data i am recieving.I want to get the value of datetime,accesskey and accesssecret in apex
{"hotelogix":{"version":"1.0","datetime":"2019-11-13T05:40:14","response":{"status":{"code":0,"message":"success"},"accesskey":"0vi1yR7ssx4IMrw","accesssecret":"rbgTdw6kfpE8ZQV"},"request":{"method":"wsauth","key":"5151BE0BE4A9B667C07"}}}
Best Answer chosen by PC
Khan AnasKhan Anas (Salesforce Developers) 
Hi Puja,

Greetings to you!

This tool will parse the JSON for you and create a class: http://json2apex.herokuapp.com/
which results in this:
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

	public class Status {
		public Integer code;
		public String message;
	}

	public class Response {
		public Status status;
		public String accesskey;
		public String accesssecret;
	}

	public Hotelogix hotelogix;

	public class Request {
		public String method;
		public String key;
	}

	public class Hotelogix {
		public String version;
		public String datetime;
		public Response response;
		public Request request;
	}

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

And then you can parse the JSON and you can reference it in code. 
Try this in Execute Anonymous Window:
String jsonString='{"hotelogix":{"version":"1.0","dt":"2019-11-13T05:40:14","response":{"status":{"code":0,"message":"success"},"accesskey":"0vi1yR7ssx4IMrw","accesssecret":"rbgTdw6kfpE8ZQV"},"request":{"method":"wsauth","key":"5151BE0BE4A9B667C07"}}}';
JsonParse obj = JsonParse.parse(jsonString);
System.debug(obj.hotelogix.dt);
System.debug(obj.hotelogix.Response.accesskey);
System.debug(obj.hotelogix.Response.accesssecret);

Alternatively, you can use System.JSON.deserializeUntyped(json); as suggested by Chandrika.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

Devi ChandrikaDevi Chandrika (Salesforce Developers) 
Hi puja,
Here you have to deserialize the JSON string inorder to access it in apex.For this use deserialize methods of JSON class.

Please refer this links which might help you further
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm
https://developer.salesforce.com/forums/?id=906F0000000kE2DIAU

Hope this helps you
If this helps kindly mark it as solved so that it may help others in future.

Thanks and Regards
Khan AnasKhan Anas (Salesforce Developers) 
Hi Puja,

Greetings to you!

This tool will parse the JSON for you and create a class: http://json2apex.herokuapp.com/
which results in this:
//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class JSON2Apex {

	public class Status {
		public Integer code;
		public String message;
	}

	public class Response {
		public Status status;
		public String accesskey;
		public String accesssecret;
	}

	public Hotelogix hotelogix;

	public class Request {
		public String method;
		public String key;
	}

	public class Hotelogix {
		public String version;
		public String datetime;
		public Response response;
		public Request request;
	}

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

And then you can parse the JSON and you can reference it in code. 
Try this in Execute Anonymous Window:
String jsonString='{"hotelogix":{"version":"1.0","dt":"2019-11-13T05:40:14","response":{"status":{"code":0,"message":"success"},"accesskey":"0vi1yR7ssx4IMrw","accesssecret":"rbgTdw6kfpE8ZQV"},"request":{"method":"wsauth","key":"5151BE0BE4A9B667C07"}}}';
JsonParse obj = JsonParse.parse(jsonString);
System.debug(obj.hotelogix.dt);
System.debug(obj.hotelogix.Response.accesskey);
System.debug(obj.hotelogix.Response.accesssecret);

Alternatively, you can use System.JSON.deserializeUntyped(json); as suggested by Chandrika.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Puja,

I have gone through your question. There is a class named 'JSONParser' in salesforce that can be used to deserialize JSON data.
Please check the link below for details - 

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_json_jsonparser.htm


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi
www.ajaydubedi.com