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
anu35anu35 

JSON.deserializeUntyped Code Coverage in apex

Hi All,
am unable to do test coverage " Map<String,Object> jsonStub = (Map<String,Object>)JSON.deserializeUntyped(bodyResp); "  getting error as "System.JSONException: Illegal unquoted character ((CTRL-CHAR, code 13)): has to be escaped using backslash to be included in string value at [line:1, column:3254] " Can any one help me on this.
Thanks
Best Answer chosen by anu35
Akhil AnilAkhil Anil
Hi Anu,

You will have to trim off all the special characters in the response like this
 
String replaceIllegal= res.getbody().replaceAll('\n','').replaceAll('\r','');
Map<String,Object> responsemap = (Map<String,Object>)JSON.deserializeUntyped(replaceIllegal);

Kindly mark it as an answer if that works !

All Answers

Akhil AnilAkhil Anil
Hi Anu,

You will have to trim off all the special characters in the response like this
 
String replaceIllegal= res.getbody().replaceAll('\n','').replaceAll('\r','');
Map<String,Object> responsemap = (Map<String,Object>)JSON.deserializeUntyped(replaceIllegal);

Kindly mark it as an answer if that works !
This was selected as the best answer
Vijay NagarathinamVijay Nagarathinam
Try the below it will resolve your issue.
 
String replaceIllegal= res.getbody().replaceAll('\n','').replaceAll('\r','');
ConsoleWrapperList=(List<demomailsentwrap>)System.JSON.deserialize(replaceIllegal,List<demomailsentwrap>.class);

Thanks,
Vijay
anu35anu35
But am not calling JSON.deserializeUntyped in Test class, it is in method so am passing the parameters through static method,it has to work but getting an error.
Akhil AnilAkhil Anil
Hi Anu,

You will have to change the Original class and NOT your test class. Then when you call the main class method from the test class, you should no longer see the error.