Tuesday, June 30, 2015

Parsing JSON in Apex

This article will explain how to parse JSON content and deserialize  JSON into Apex objects.

Use the JSONParser methods to parse a response that's returned from a call to an external service that is in JSON format, such as a JSON-encoded response of a Web service call out.

There are 2 ways you can parse JSON in apex. We will cover both option and their benefits.


1. Parsing JSON by inspecting each JSONToken present in JSON


Generally this method is useful for grabbing specific pieces of data from JSON. The System.JSONToken is an enumerator that provides values such as FIELD_NAME, START_OBJECT, END_OBJECT and others that inform you of the type of token currently being parsed.
Below is sample JSON (JSON response from callout to Box.com) which we will be using here for parsing.

{  
   "type":"session",
   "id":"d1425e031062455f909740bb770b95a7",
   "document":{  
      "type":"document",
      "id":"ab0dc2d377524f44a98fd1476343637e",
      "status":"processing",
      "name":"",
      "created_at":"2015-06-19T08:35:09Z"
   },
   "expires_at":"2015-06-19T09:35:10.020Z",
   "urls":{  
      "view":"https://view-api.box.com/1/sessions/d1425e031062455f909740bb770b95a7/view",
      "assets":"https://view-api.box.com/1/sessions/d1425e031062455f909740bb770b95a7/assets/",
      "realtime":"https://view-api.box.com/sse/d1425e031062455f909740bb770b95a7"
   }
}

Now suppose if we have to fetch the session id from JSON, then we will be using below code:

HTTPResponse res=sendHttpRequest(req);
String response=res.getBody();
String SessionId='';
JSONParser  parser = JSON.createParser(Response);
while (parser.nextToken() != null) {
if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
String fieldName = parser.getText();
parser.nextToken();
String fieldValue=parser.getText();
if(fieldName == 'type' && fieldValue=='session') {
parser.nextToken();
if(parser.getText()=='id'){
parser.nextToken();
SessionId= parser.getText();
break;
}
}
}
}

The code above executes a while loop that walks through the entire JSON string using the parser.nextToken() method and parses simple field text values such as id or session using the parser.getText() method.

If we need only specific information like session id, then we will check token value as id after type and session token. After getting the session id, we are breaking the loop so that we are not getting any other token with value id in JSON.

Points to remember while using this approch:
  • If you want specific information from JSON, then use this approach instead of storing all JSON information in apex objects.
  • Developer has to check different tokens in order to get required value from JSON.


2. Parsing JSON by deserializing It into Object


In this approch, we are going create a apex class and deserialize the JSON to store information in apex class variable. This approach is very simple but the only thing is that you need to defined Apex classes mapped to each of the element/entity represented in the JSON string.

You can refer below URL in order to generate apex class structure for JSON.

http://json2apex.herokuapp.com/


Paste your JSON in this tool and it will generate required apex class for deserializing the JSON. Below is class generate for above sample JSON response:




You can define getter setter methods for variables if you want to display on VF page like:
public String type {get;set;}
public String type {get;set;}

If we are using inner class to store values, then we can remove the parse method and testmethod from above generated apex code. Below is simplified version of class which we can be used to deserialize JSON:

public class JSON2Apex {
public class Document {
public String type;
public String id;
public String status;
public String name;
public String created_at;
}
public class Urls {
public String view;
public String assets;
public String realtime;
}
public String type;
public String id;
public Document document;
public String expires_at;
public Urls urls;
}

Now we have apex class required to deserialize the JSON String. We can deserialize the JSON with single line of code in apex mentioned below:


HTTPResponse res=sendHttpRequest(req);

String response=res.getBody();
JSON2Apex   JSONDetails=(JSON2Apex) System.JSON.deserialize(response, JSON2Apex.class);

Now you will have all information from JSON into apex class variables.


Points to remember while using this approach:
  • It is not required that you have to create variables for all elements/entity in apex class. In our example, if you don't want fetch urls information from JSON, then you can remove URLs public class. from JSON2Apex class. Also you can remove other variables like type, created_at etc from JSON2Apex class if not required in your apex class.
  • For multi-level nested JSON, you are limited by the depth of inner class as you can only have one level of inner class in apex. If you need to parse deeper nested JSON, then use parsing through tokens.



5 comments:

  1. how to do test coverage for this approach

    SONParser parser = JSON.createParser(Response);
    while (parser.nextToken() != null) {
    if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)){
    String fieldName = parser.getText();
    parser.nextToken();
    String fieldValue=parser.getText();
    if(fieldName == 'type' && fieldValue=='session') {
    parser.nextToken();
    if(parser.getText()=='id'){
    parser.nextToken();
    SessionId= parser.getText();
    break;

    ReplyDelete
  2. I would recommend to use JSON tool for debugging JSON data http://codebeautify.org/jsonviewer

    ReplyDelete
  3. Very nice article and these tools will help JSON lovers http://jsonformatter.org and http://codebeautify.org/jsonviewer

    ReplyDelete

  4. Hi everybody, I hope you are doing good. Dear admin I had scroll down your site and found it really useful for web development tools. Since I am a developer, I most often refer people to use best website in the town for web development. Your efforts are really appreciable. We also provide web development tool. Here is the link of our site jsononline


    ReplyDelete