am trying to parse the below JSON request to Apex class. The challenge here is that one of the node "match" in the JSON is suppose to be an array but the external system sends it as a String. I would need to convert the string to array and parse it. The approach that I followed was to remove the double quotes for the match node and use JSOn2Apex heroku app - https://json2apex.herokuapp.com/ to generate the response Apex class and use JSON.deserialize(jsonString, apexType) to deserialize the result.
{
"response": [
{
"tmodule": "trans",
"resultList": [
{
"output": [
{
"var1": 8119,
"var2": "Foundational",
"var3": "",
"match": "[{\"type\":\"Record\",\"tableName\":\"model_score\",\"AnId\":\"103708\",\"frequency\":\"Weekly\",\"Type\":\"trigger\",\"country\":\"Argentina,Australia,Austria,Canada,Finland,France,Germany,HongKong,India,Italy,Japan,Mexico,Netherlands,NewZealand,Singapore,Spain,Sweden,Taiwan,Thailand,UK,US\",\"fields\":[{\"name\":\"wwwf\",\"type\":\"sdad\",\"alias\":\"\",\"doc\":\"CM15 represents the 15 digit card number embossed on the plastic card\"},{\"name\":\"Score\",\"type\":\"Double\",\"alias\":\"\",\"doc\":\"Score value generated from the execution of the model\"},{\"name\":\"outp_var1\",\"type\":\"Double\",\"alias\":\"\",\"doc\":\"amount\"},{\"name\":\"outp_var3\",\"type\":\"String\",\"alias\":\"SegmentId\",\"doc\":\"modelId\"}],\"segmentIdColumn\":\"output_var3\",\"segmentIdDetails\":[{\"segmentId\":\"SM1\",\"description\":\"amount>=100000 and amount<115000\"},{\"segmentId\":\"SM2\",\"description\":\"amount>=100000 and amount<150000\"},{\"segmentId\":\"SM3\",\"description\":\"Other\"}]}]",
"table": "stname",
"create_date": 3243252345
}
],
"SId": 1234,
"Sdescp": "XXXXXX"
}
]
}
],
"error": false,
"errorResponse": null
}
However I had trouble with parsing the "match" node for which I had to tweak the response apex class a bit like this. I had to add another parser to replace the double quotes. For some reason it worked. However I was not able to use JSON.deserialize(jsonString, apexType) to deserialize the JSON instead I had to call the Parse method directly which is in Response Apex Class.
private static List<Match> arrayOfMatch(System.JSONParser parseObj) {
List<Match> res = new List<Match>();
if (parseObj.getCurrentToken() == null){
parseObj.nextToken();
}
System.JSONParser parser2 = System.JSON.createParser(parseObj.getText().replace('\\',''));
while (parser2.nextToken() != System.JSONToken.END_ARRAY) {
res.add(new Match(parser2));
}
return res;
}
Is there a better way to do this? Given how the JSON attribute match is represent as a String instead of an array. What is the best approach in this case.