0

Today I was working extracting information from a JSON object. I'm using jsonToken to navigate and get the values. When I have a JSON with 300 or less objects, I have no problem but when the JSON has more than 700 I always have the "Apex CPU limit exceeded" error.

After commenting a lot of code, here is what remains:

while(parser.nextToken() != null) {
    if(parser.getCurrentToken() == JsonToken.FIELD_NAME) {
        //fd.Name = parser.getText();
        continue;
    }
    if(parser.getCurrentToken() == JsonToken.VALUE_STRING && fd.Name != null) {
        //fd.Value = parser.getText();
        //po.FieldAndValue.add(fd);
        //fd = new FieldDetail();
        continue;
    }
    if(parser.getCurrentToken() == JsonToken.START_OBJECT) {
        //fd = new FieldDetail();
        continue;
    }
    // Once we know end of object, we can start a new record.
    if(parser.getCurrentToken() == JsonToken.END_OBJECT) {
        //parcelLeads.add(generateParcelLead(po));
        //parcelLeadJSONInfo.add(po);
        //po = new ParsedObject();
        continue;
    }
}

As you see I'm doing nothing other than reading the JSON but still get the governor error. Is there any other way to extract the values? (map or parser.readValueAs cannot be used).

The Json is like this:

{
    "2017-04-28_IL_Macoupin_MISO_DG":
    [
        {
            "FID": "3",
            "site_id": "IL_Site_16042",
            "lomp_id": " " //, ... 64 more Keypairs here
        }
    ]
}

In this example, there is only 1 record and 3 fields, there are 700 records and 67 fields in the real object.

8
  • 2
    Also, is there a reason why you're not parsing this into a list and using that to evaluate? Jul 19, 2017 at 16:01
  • Please edit your post to add any useful information, including the payload.
    – Adrian Larson
    Jul 19, 2017 at 16:03
  • 3
    Don't use JSONParser. It's the least efficient means of processing JSON, and primarily provided so you can port Java code (more or less) directly to Apex.
    – sfdcfox
    Jul 19, 2017 at 16:03
  • @sfdcfox wich other class I could use? as I have seen, this is the only Library that let me read the Name and Value of the keypair of the json. Jul 19, 2017 at 17:00
  • 1
    @PiojoNieto JSON.deserialize and JSON.deserializeUntyped are better alternatives. Use the former if there's no reserved keywords, or the latter otherwise.
    – sfdcfox
    Jul 19, 2017 at 17:08

0

You must log in to answer this question.

Browse other questions tagged .