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
pooja chauchanpooja chauchan 

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

1) Create some Sort of schedule in Salesforce?
2)  read chunk of data and insert/update?
Best Answer chosen by pooja chauchan
Gaurav NirwalGaurav Nirwal

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.