Friday, 9 August 2013

JSON Parser Example

Hi,
Here i am going to explain how to parse JSON string.
Some times we get data from external app in the form JSON.
In real time we need to parse JSON data comes from external app and insert into salesforce objects.For that we need to parse JSON data and insert them into database object as records.
For that we need to write the wrapper class with key names in json string we got and then by using json parser class we can do parsing as shown below.

In the following example i have a JSON string with lead data having firstname,lastname,company,city,state etc,
I prepared a wrapper class with different properties for parsing.

Controller:
===========
public  class Jsonstringparser {
public String jsonstring{get;set;}
public List<Lead> lstlead{get;set;}
public Jsonstringparser(){
jsonstring='[{"FirstName":"Balaji","LastName":"Malemarpuram","Company":"Oracle","City":"Hyderabad","State":"AP","PostalCode":500081,"Phone":9502026343,"Status":"Open - Not Contacted","Tags":[]},{"FirstName":"Sreevardhan","LastName":"Malemarpuram","Company":"Appshark","City":"Hyderabad","State":"AP","PostalCode":500081,"Phone":95055556343,"Status":"Open - Not Contacted","Tags":[]}]';
lstlead=new List<Lead>();
}
public void parseJsonString(){
JSONParser parser = JSON.createParser(jsonstring);
 while (parser.nextToken() != null) {
  if (parser.getCurrentToken() == JSONToken.START_ARRAY) {  
  while (parser.nextToken() != null) {
   if (parser.getCurrentToken() == JSONToken.START_OBJECT) {
    Jsonparsercls le = (Jsonparsercls)parser.readValueAs(Jsonparsercls.class);
    Lead leadobj=new Lead();
    leadobj.FirstName=le.FirstName;
    leadobj.LastName=le.LastName;
    leadobj.Company=le.Company;
    leadobj.City=le.City;
    leadobj.State=le.State;
    leadobj.PostalCode=le.PostalCode;
    leadobj.Phone=le.Phone;
    leadobj.Status=le.Status;
    lstlead.add(leadobj);
   }
  }
  }
 }
 if(lstlead!=null && lstlead.size()>0)
  insert lstlead;
}
public class Jsonparsercls{
public String FirstName{get;set;}
public String LastName{get;set;}
public String Company{get;set;}
public String City{get;set;}
public String State{get;set;}
public String PostalCode{get;set;}
public String Phone{get;set;}
public String Status{get;set;}
}

}
================
Parser Page for inserting into lead
====================<apex:page controller="Jsonstringparser" sidebar="false">
<apex:form >
<div style="color:#FFFAF0;background-color:#808000;font-weight:bold;">JSON String</div>
<div  style="color:#000080;background-color:#EEE8AA;width:100%;height:100px;">
<br/><br/>
<center>{!jsonstring}</center>
</div>
<apex:pageBlock >
<apex:pageBlockButtons location="top">
<apex:commandButton value="Parse & Insert" action="{!parseJsonString}"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!lstlead}" var="le">
<apex:column headerValue="First Name" value="{!le.FirstName}"/>
<apex:column headerValue="Last Name" value="{!le.LastName}"/>
<apex:column headerValue="Company" value="{!le.Company}"/>
<apex:column headerValue="City" value="{!le.City}"/>
<apex:column headerValue="State" value="{!le.State}"/>
<apex:column headerValue="PostalCode" value="{!le.PostalCode}"/>
<apex:column headerValue="Phone" value="{!le.Phone}"/>
<apex:column headerValue="Lead Status" value="{!le.Status}"/>
</apex:pageBlockTable>
</apex:pageBlock>
<br/><br/><br/><br/><br/><br/><br/><br/>
</apex:form>
</apex:page>
============

Output:
======



5 comments:

  1. that it help me a lot as i am first time with JSON & SF

    ReplyDelete
  2. Nice one.. helped me a lot too... thanks...

    ReplyDelete
  3. Thanks for the Post... very helpful

    ReplyDelete
  4. awesome! Good explanation. Very helpfull.

    ReplyDelete
  5. awesome explanation... It really help me a lot

    ReplyDelete