+ Start a Discussion
Sam1980Sam1980 

Apex :: Best Way to Read And Insert/Update Large JSON Data

I have a JSON feed  that i want to read and insert into Contact and Companies Object.

Its Very Large JSON Data. What is the best way to do it ? Shall i

1) Create some Sort of schedule in Salesforce?
2)  read chunk of data and insert/update?

Any code would be helpful

Here is my Json Feed url http://tinyurl.com/mpnb2fo
Best Answer chosen by Sam1980
kaustav goswamikaustav goswami
Do you have a class that represents this json through some sort of object?

Is yes then you can use the de-serialize method

Say you have a class called that represents the json content in object format.

public class CompanyContactClass{
    List<Contact> conList;
    List<Company> compList;
    .....
    .....
}

If the Json conforms to that class format then you can do something like this to parse it without writing much code and without iterating over loops.

String jSonString = 'you json string';

CompanyContactClass classInstance = (CompanyContactClass) JSON.deserializeStrict(jSonString, CompanyContactClass.class);

This will gove you the parsed json in the instance of the class that you have. Now you can use the object to access the data.

The other way is to read the json token by token and access its content.

Sample code - 

String JSONContent =
   '{"arrivalTime":"18:05"}';
JSONParser parser =
   JSON.createParser(JSONContent);
// Advance to the start object marker.
parser.nextToken();
// Advance to the next value.
parser.nextValue();
// Get the arrival time.
Time arrivalTime = parser.getTimeValue();

In the above snippet the getTimeValue will be replaced by whatecer data type you are trying to read from the json.

In order to access the data in this way you will have to iterate ovet the entire json and access the contents one by one.

Thanks,
Kaustav