I'm not sure I understand your question. By "without calling out" and "can get response from other callback url" do you mean that you want to publish an endpoint that will accept JSON that is posted to it? If so you can create an @RestResource Apex class with a @HttpPost method that can hanle most of the work - including deserialization from a JSON String to Apex objects - for you.
But if you have the JSON string available and are just asking how to parse it, there are lots of tools to help you parse it into Apex objects. In the JSON class:
- JSON.deserializeStrict(String jsonString, System.Type apexType)
- JSON.deserialize(String jsonString, System.Type apexType)
- JSON.deserializeUntyped(String jsonString)
- JSON.createParser(String jsonString)
Which of these JSON methods you choose depends on how fixed (known up front) the format of the JSON is. I've listed the methods in order of "best for fixed" though to "best for not fixed".
PS
From your comments below it sounds like you want to setup a REST endpoint in Salesforce that your "call centre website" can invoke. (The process will have to include authentication.) A @RestResource Apex class works well for this and would look something like this:
@RestResource(urlMapping='/myName')
global with sharing class MyRest {
@HttpPost
global static String doPost(...) {
// Apex code that does something with the posted data
}
}
where ... is up to you: zero or more primitive values, simple Apex classes, SObjects, arrays of any of these. The framework automatically deserializes the JSON based on the parameter list.
To provide an example, this JSON:
{"abc": "some message", "def": [1, 2, 3]}
would be automatically deserialized by this code:
@HttpPost
global static String doPost(String abc, Integer[] def) {
// abc will be "some message"
// def will be the Integer array 1, 2, 3
}
You mention "it should be dynamic" but I am not sure what you mean by that.
As sfdcfox suggested in a comment, you can use json2apex to generate the necessary code from a JSON sample. The app is a little out of date in that test methods should now be in a different class, so I generated the code and separated out the test method:
UserAgreements Class
public class UserAgreements {
public class DisplayUserInfo {
public String company;
public String fullNameOrEmail;
}
public List<UserAgreementList> userAgreementList;
public class UserAgreementList {
public String displayDate;
public DisplayUserInfo displayUserInfo;
public Boolean esign;
public String agreementId;
public String latestVersionId;
public String name;
public String status;
}
public static UserAgreements parse(String json) {
return (UserAgreements) System.JSON.deserialize(json, UserAgreements.class);
}
}
UserAgreementsTest Class
@isTest
public class UserAgreementsTest {
static testMethod void testParse() {
String json = '{'+
' \"userAgreementList\": ['+
' {'+
' \"displayDate\": \"2015-04-02T09:32:23-07:00\",'+
' \"displayUserInfo\": {'+
' \"company\": \"TEST\",'+
' \"fullNameOrEmail\": \"Tim 1234\"'+
' },'+
' \"esign\": true,'+
' \"agreementId\": \"AGREEMENT ID\",'+
' \"latestVersionId\": \"VERSION ID\",'+
' \"name\": \"Agreement for APEX Test Account - 2015-04-02\",'+
' \"status\": \"OUT_FOR_SIGNATURE\"'+
' },'+
' {'+
' \"displayDate\": \"2015-04-02T03:32:57-07:00\",'+
' \"displayUserInfo\": {'+
' \"company\": \"TEST\",'+
' \"fullNameOrEmail\": \"Tim 1234\"'+
' },'+
' \"esign\": true,'+
' \"agreementId\": \"AGREEMENT ID\",'+
' \"latestVersionId\": \"VERSION ID\",'+
' \"name\": \"Agreement for APEX Test Account - 2015-04-02\",'+
' \"status\": \"OUT_FOR_SIGNATURE\"'+
' },'+
' {'+
' \"displayDate\": \"2015-03-31T09:15:11-07:00\",'+
' \"displayUserInfo\": {'+
' \"company\": \"TEST\",'+
' \"fullNameOrEmail\": \"Tim 1234\"'+
' },'+
' \"esign\": true,'+
' \"agreementId\": \"AGREEMENT ID\",'+
' \"latestVersionId\": \"VERSION ID\",'+
' \"name\": \"Agreement for APEX Test Account - 2015-04-02\",'+
' \"status\": \"RECALLED\"'+
' }'+
' ]'+
'}';
UserAgreements obj = UserAgreements.parse(json);
System.assert(obj != null);
}
}
Best Answer
Take alook at the JSON2Apex app (credits to @metadaddy and & @superfell)
You basically paste in your json, the tool generates a apex class which you can use to easily parse your json, and access data from attributes instead of parsing the json manually.
This would be the result for your json.