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
KaityKaity 

JSON in APEX

Hello everybody,
When do we use JSON in APEX? Can some one explain elaborately. Many Thanks.

-Kaity
Best Answer chosen by Kaity
pconpcon
For example, if you were making an http GET request you could do
 
public class HttpCallout {
    public class HttpIP {
        public String ip;
    }

    public static HttpIP getIP() {
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://ip.jsontest.com');
        req.setMethod('GET');

        HttpResponse res = h.send(req);
        return (HttpIP) JSON.deserializeStrict(res.getBody(), HttpIP.class);
    }
}

this would deserialize into your HttpIP class and give you the IP.  You could then do the following in a non-trigger class
 
String ip = HttpCallout.getIP().ip;

Similarly we could take an existing class, serialize it and send it as a POST
 
public class HttpCallout {
    public class HttpValidResult {
        public String object_or_array;
        public Boolean empty;
        public long parse_time_nanoseconds;
        public Boolean validate;
        public long size;
    }
   
    public static Boolean isValidJSON(String json) {
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://validate.jsontest.com');
        req.setMethod('POST');
        req.setBody(json);

        HttpResponse res = h.send(req);
        HttpValidResult result = (HttpValidResult) JSON.deserializeStrict(res.getBody(), HttpValidResult.class);
        return result.validate;
    }
}

The we call this by saying
 
public class TestClass {
    public String foo;
    public Integer iFoo;
}

TestClass t = new TestClass(
    foo = 'bar',
    iFoo = 8
);

Boolean isValid = HttpCallout.isValidJSON(JSON.serialize(t));

NOTE: You will have to add the endpoint to your Remote Site Details to be able to make the callout against it

For more information read the following articles:

https://developer.salesforce.com/page/Apex_Web_Services_and_Callouts
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http.htm
http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_json.htm

All Answers

pconpcon
I'm not sure what your question means.  You can use JSON in lots of places inside of Apex.  If you are making a remote callout to a third party system that returns JSON you can use it there.  You can serialize/deserialize object in and out of JSON.  If you are only working inside the Salesforce system, there is typically no reason to work with JSON.  You can do some more advanced things like logging status of objects with it, but it's not a normal thing to do.

If this doesn't answer your question, can you please expand upon what you are trying to do with JSON and Apex?
KaityKaity
Hi pcon,
Thanks for replying. You correctly clear my doubt. Can you please elaborate this- 
"If you are making a remote callout to a third party system that returns JSON you can use it there.  You can serialize/deserialize object in and out of JSON."

Can you please give a sample code for this? Third party returns JSON, how can i understand that. Please help. Many Thanks..

-Kaity
pconpcon
For example, if you were making an http GET request you could do
 
public class HttpCallout {
    public class HttpIP {
        public String ip;
    }

    public static HttpIP getIP() {
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://ip.jsontest.com');
        req.setMethod('GET');

        HttpResponse res = h.send(req);
        return (HttpIP) JSON.deserializeStrict(res.getBody(), HttpIP.class);
    }
}

this would deserialize into your HttpIP class and give you the IP.  You could then do the following in a non-trigger class
 
String ip = HttpCallout.getIP().ip;

Similarly we could take an existing class, serialize it and send it as a POST
 
public class HttpCallout {
    public class HttpValidResult {
        public String object_or_array;
        public Boolean empty;
        public long parse_time_nanoseconds;
        public Boolean validate;
        public long size;
    }
   
    public static Boolean isValidJSON(String json) {
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://validate.jsontest.com');
        req.setMethod('POST');
        req.setBody(json);

        HttpResponse res = h.send(req);
        HttpValidResult result = (HttpValidResult) JSON.deserializeStrict(res.getBody(), HttpValidResult.class);
        return result.validate;
    }
}

The we call this by saying
 
public class TestClass {
    public String foo;
    public Integer iFoo;
}

TestClass t = new TestClass(
    foo = 'bar',
    iFoo = 8
);

Boolean isValid = HttpCallout.isValidJSON(JSON.serialize(t));

NOTE: You will have to add the endpoint to your Remote Site Details to be able to make the callout against it

For more information read the following articles:

https://developer.salesforce.com/page/Apex_Web_Services_and_Callouts
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http.htm
http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_json.htm
This was selected as the best answer
KaityKaity
Thank You pcon for the clarification. I appreciate.
-Kaity