Here I have to replace 6 keywords from the response. I have created a metadata with word and replaced word.
public static String replaceJSONKeywordsModified(String strJsonResponse) {
String strJson = '';
//strRegexFormat is a regular expression used to replace only the keys in JSON string
String strRegexFormat = '(?i)(?m)^\\s*"{0}"\\s*:';
String strReplacementFormat = '"{0}" :';
String strRegex;
String strReplacement;
String strFormattedJSON;
List<Reserve_Keyword_ACE__mdt> lstReserveKeywords = new List<Reserve_Keyword_ACE__mdt>();
try {
if (String.isNotEmpty(strJsonResponse)) {
lstReserveKeywords = [SELECT MasterLabel,DeveloperName FROM Reserve_Keywords__mdt WHERE Active_ACE__c = true];
strFormattedJSON = JSON.serializePretty(JSON.deserializeUntyped(strJsonResponse));
for (Integer intCount = 0; intCount < lstReserveKeywords.size(); intCount++) {
strRegex = String.format(
strRegexFormat,
new List<String>{
lstReserveKeywords[intCount].MasterLabel
}
);
strReplacement = String.format(
strReplacementFormat,
new List<String>{
lstReserveKeywords[intCount].DeveloperName
}
);
strFormattedJSON = strFormattedJSON.replaceAll(strRegex, strReplacement);
}
strJson = strFormattedJSON;
} else {
strJson = strJsonResponse;
}
} catch (JSONException objException) {
strJson = strJsonResponse;
}
return strJson;
}
Above is the logic which replaces all the reserved keywords by iterating over the reserved keywords.
The issue here is this logic breaks if the response is big. regex too complicated
If I can split the response in 4 equal parts, based on the response length, and then pass each part through this method then it can resolve the issue.
Or is there any other way I can achieve this without hitting Apex CPU time limit?
Also how can I split the response in different parts?
JSONResponseObject.get('systemReservedWord')you expect the system reserved keywords to be present somewhere, I did not understand where. Secondly, I am not sure about thisnew JSONChildObject(JSONResponseObject.get('childProp')), asJSONResponseObject.get('childProp')would return a List and there is no constructor for that inJSONChildObject, Also isJSONChildObjectconstucting the reserved keywords? – Nagendra Singh Aug 21 '19 at 03:06