[SalesForce] How to get Json response without rest api and parse the response in objects

I need to get json response and parse the response into the object(Without using http callout).I need to have application(class) which can get response from other callback url and parse that response into objects.

Best Answer

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.