Skip to content

Instantly share code, notes, and snippets.

@balindersingh
Created December 14, 2023 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balindersingh/a45febcbdcbb19f6ff8ddfd7a3a61a8f to your computer and use it in GitHub Desktop.
Save balindersingh/a45febcbdcbb19f6ff8ddfd7a3a61a8f to your computer and use it in GitHub Desktop.
Parse JSON string in Salesforce FLOW using Invocable Action
/*
* @input (Request object):
* @param: JSON String to Parse ==>
@example:
{
"Charges": [
{
"CardDetail": {
"Brand": "visa",
"ExpMonth": 2,
"ExpYear": 2042,
"Last4": "4242"
},
"Id": "<charge_id>",
"PaymentMethodDetailType": "card",
"ReceiptUrl": "<ReceiptUrl>",
"Status": "succeeded"
}
],
"NextAction": "",
"PaymentIntentId": "<stripe_payment_intent_id>",
"PaymentMethodId": "<stripe_payment_method_id>",
"Status": "succeeded"
}
* @params: Key<number> ==> field path in json
@example:
(Key1) Status
(Key2) Charges.[0]CardDetail.ExpMonth
(Key3) Charges.[0]CardDetail.ExpYear
(Key4) Charges.[0]CardDetail.Last4
(Key5) Charges
* @return (ResponseValue object):
* @params: Value<number> ==> Variables which will be populated based on above Keys. Given above keys values will be like following
@example:
(Value1) succeeded
(Value2) 2
(Value3) 2042
(Value4) 4242
(Value5) [{"CardDetail":{"Brand":"visa","ExpMonth":2,"ExpYear":2042,"Last4":"4242"},"Id":"<charge_id>","PaymentMethodDetailType":"card","ReceiptUrl":"<ReceiptUrl>","Status":"succeeded"}]
*/
public class JSONParserInvocableAction {
@InvocableMethod(label='Lite Pae JSON Parser Action' description='Sample method to parse JSON reponse')
public static List<ResponseValue> JSONParserInvocableAction(List<Request> jsonObjects) {
List<ResponseValue> results = new List<ResponseValue>();
ResponseValue result = new ResponseValue();
for (Request jsonObject : jsonObjects) {
Object jsonToParse = JSON.deserializeUntyped(jsonObject.StringToParseInJSONFormat);
result.Value1 = ParseJSON(jsonObject.Key1, jsonToParse);
result.Value2 = ParseJSON(jsonObject.Key2, jsonToParse);
result.Value3 = ParseJSON(jsonObject.Key3, jsonToParse);
result.Value4 = ParseJSON(jsonObject.Key4, jsonToParse);
result.Value5 = ParseJSON(jsonObject.Key5, jsonToParse);
results.add(result);
}
return results;
}
private static String ParseJSON(String fieldName, Object jsonToParse) {
String result;
if (fieldName != null) {
try {
result = mapParser(fieldName, jsonToParse);
} catch (exception e) {
result = '[ParseJSON] [Exception] : ' + e.getMessage() + ' ==> ' + e.getStackTraceString();
}
} else {
System.debug('[ParseJSON] Nothing to parse');
}
return result;
}
private static String mapParser(String fieldName, Object jsonObject) {
Object paramObject = null;
Object JSONString = JSON.serialize(jsonObject);
System.debug('[mapParser] JSONString: ' + JSONString);
if (jsonObject instanceof List<Object> && fieldName != '') {
System.debug('[mapParser] List:' + fieldName);
List<Object> theList = (List<Object>) jsonObject;
Integer enclosureIndex = fieldName.indexOf(']');
Integer listIndex = Integer.valueOf(fieldName.substring(1, enclosureIndex));
paramObject = theList.get(listIndex);
fieldName = fieldName.substring(enclosureIndex + 1);
return mapParser(fieldName, paramObject);
}
if (jsonObject instanceof Map<String, Object> && fieldName != '') {
System.debug('[mapParser] Map:' + fieldName);
Map<String, Object> theMap = (Map<String, Object>) jsonObject;
Integer dotIndex = fieldName.indexOf('.');
if (dotIndex < 0) {
dotIndex = fieldName.length();
}
Integer listIndex = fieldName.indexOf('[');
if (listIndex < 0) {
listIndex = fieldName.length();
}
Integer earliestIndex = math.min(dotIndex, listIndex);
String traversedFieldName = fieldName.substring(0, earliestIndex);
paramObject = theMap.get(traversedFieldName);
if (earliestIndex + 1 > fieldName.length()) {
fieldName = '';
} else {
fieldName = fieldName.substring(earliestIndex + 1);
}
return mapParser(fieldName, paramObject);
} else {
if (jsonObject instanceof Map<String, Object> || jsonObject instanceof List<Object>) {
System.debug('[mapParser] Either Map<String, Object> or List<Object>');
return getStringValue(JSONString);
} else {
System.debug('[mapParser] Not Map<String, Object> or List<Object>');
return getStringValue(jsonObject);
}
}
}
private static String getStringValue(Object paramObject) {
String result = null;
if (paramObject != null) {
result = String.valueOf(paramObject);
}
return result;
}
public class Request {
@InvocableVariable(label='String to Parse (in valid JSON format)' required=true description='JSON String to Parse')
public String StringToParseInJSONFormat;
@InvocableVariable(label='Key1')
public String Key1;
@InvocableVariable(label='Key2')
public String Key2;
@InvocableVariable(label='Key3')
public String Key3;
@InvocableVariable(label='Key4')
public String Key4;
@InvocableVariable(label='Key5')
public String Key5;
}
public class ResponseValue {
@InvocableVariable(label='Value1')
public String Value1;
@InvocableVariable(label='Value2')
public String Value2;
@InvocableVariable(label='Value3')
public String Value3;
@InvocableVariable(label='Value4')
public String Value4;
@InvocableVariable(label='Value5')
public String Value5;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment