Parsing Instagram JSON on Apex

Last week I had a challenge of making a API callout to Instagram from Salesforce and then parse the returned JSON content and the mapped the values to become a record for a custom object. Basically each media post and comments becomes a record in Salesforce.

First off you can start by reading this article

According to http://www.json.org:

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. … JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.

To begin, what I have here is an API request for comments for a particular media or picture I have in Instagram.

The returned JSON object looks like this.

{“meta”:{“code”:200},”data”:[{“created_time”:”1389149619″,”text”:”Penge po.. :-)”,”from_user”:{“username”:”anjela317″,”profile_picture”:
“http:\/\/images.ak.instagram.com\/profiles\/profile_280731589_75sq_1362457959.jpg”,
“id”:”280731589″,”full_name”:”Angela Dulay”},”id”:”628555023537193060″},{“created_time”:”1389157056″,”text”:”Sure iship ko na lang. 2 mins lang ubus.”,”from_user”:{“username”:”olopsman”,”profile_picture”:
“http:\/\/images.ak.instagram.com\/profiles\/profile_38393631_75sq_1335156856.jpg”,
“id”:”38393631″,”full_name”:””},”id”:”628617413406539834″}]}

There are two approach you can do.

1. You can use the System.JSONParser and check the current token value with a while loop. Then assign manually. This could be tedious as I learned.

2. The second one is to create a wrapper class with the JSON key names which we are going to discuss.

Lucky for us. There are resources to streamline this like the website below.
You can paste the JSON and it would convert them to Apex.
http://www.adminbooster.com/tool/json2apex

You need to watch out though for reserved Apex keywords as they could be the same as the JSON key names. Based on the JSON content above. This will generate

//
//Generated by AdminBooster
//

public class fromJSON{
public cls_meta meta;
public cls_data[] data;
class cls_meta {
public Integer code; //200
}
class cls_data {
public String created_time; //1389149619
public String text; //Penge po.. 🙂
public cls_from_user from_user;
public String id; //628555023537193060
}
class cls_from_user {
public String username; //anjela317
public String profile_picture; //http://images.ak.instagram.com/profiles/profile_280731589_75sq_1362457959.jpg
public String id; //280731589
public String full_name; //Angela Dulay
}
public static fromJSON parse(String json){
return (fromJSON) System.JSON.deserialize(json, fromJSON.class);
}

}

Then on your class you can call the url end point and parse the returned data.

// Instantiate a new http object
Http h = new Http();

// Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
HttpRequest req = new HttpRequest();
req.setEndpoint(url);
req.setMethod(‘GET’);

// Send the request, and return a response
HttpResponse res = h.send(req);
userData = res.getBody();
JSONMediaComments obj = JSONMediaComments.parse(userData);
System.assert(obj != null);
system.debug(‘my obj =>’ + obj);

The obj will be an array where you can assign the values to map to your custom object’s fields.

Hope somebody finds this useful.