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
VSK98VSK98 

System.JSONException: Unexpected character ('C' (code 67)): was expecting comma to separate OBJECT entries

Hi All,

I am getting the error when I run the test class.
System.JSONException: Unexpected character ('C' (code 67)): was expecting comma to separate OBJECT entries

I have created the mock class, in that I am sending the body as below.
 
res.setBody('{"value": [{'+
        '"groupid": 1,'+
       '"group_name": "Test",'+
        '"sub_customer": "[{\"CustomerId\":\"9488491\",\"CustomerName\":\"Test\",\"Street\":\"Test\",\"City\":\"PENRITH\",\"PostalCode\":\"275\",\"Country\":\"Test\",\"Assetcnt\":\"6\"}]"}]}');

I am getting the error in Apex class
SearchResult obj=(SearchResult)JSON.deserialize(resp.getBody(), SearchResult.class);

Wrapper class :
 
Public class value{
        
        @AuraEnabled
        public Integer groupid {get; set;}
        @AuraEnabled
        public string group_name {get; set;}
        @AuraEnabled
        public String sub_customer {get; set;}
        
        
    }
    
    public class SearchResult {
        @AuraEnabled
        public List<value> value;
        
        public SearchResult(List<value> liList) {
            value = liList.clone();
        }
    } 
    
    Public class Value_out{
        @AuraEnabled
        public string group_name;
        @AuraEnabled
        public string groupid;
        //srini-added for parent level sum of assets
         @AuraEnabled
        public String sumofAssets;
        @AuraEnabled
        public list<sub_customer> children;
    }
    public class sub_customer{
        @AuraEnabled
        public string CustomerName;
        @AuraEnabled
        public string CustomerId;
        @AuraEnabled
        public string Street;
        @AuraEnabled
        public string City;
        @AuraEnabled
        public string PostalCode;
        @AuraEnabled
        public string Country;
        @AuraEnabled
        public string Assetcnt;
    }

Regards,
VSK98​​​​​​​
 
Best Answer chosen by VSK98
AmanSharma06AmanSharma06
Well json exception occurred due to incorrect json string. Tried with below json and it worked for me:

 String jsonStr ='{"value": [{'+
        '"groupid": 1,'+
       '"group_name": "Test",'+
        '"sub_customer": "[{\\"CustomerId\\":\\"9488491\\",\\"CustomerName\\":\\"Test\\",\\"Street\\":\\"Test\\",\\"City\\":\\"PENRITH\\",\\"PostalCode\\":\\"275\\",\\"Country\\":\\"Test\\",\\"Assetcnt\\":\\"6\\"}]"}]}';
SearchResult v = (SearchResult)JSON.deserialize(jsonStr, SearchResult.class);

Output :
SearchResult:[value=(value:[group_name=Test, groupid=1, sub_customer=[{"CustomerId":"9488491","CustomerName":"Test","Street":"Test","City":"PENRITH","PostalCode":"275","Country":"Test","Assetcnt":"6"}]])]
 

All Answers

AmanSharma06AmanSharma06
Well json exception occurred due to incorrect json string. Tried with below json and it worked for me:

 String jsonStr ='{"value": [{'+
        '"groupid": 1,'+
       '"group_name": "Test",'+
        '"sub_customer": "[{\\"CustomerId\\":\\"9488491\\",\\"CustomerName\\":\\"Test\\",\\"Street\\":\\"Test\\",\\"City\\":\\"PENRITH\\",\\"PostalCode\\":\\"275\\",\\"Country\\":\\"Test\\",\\"Assetcnt\\":\\"6\\"}]"}]}';
SearchResult v = (SearchResult)JSON.deserialize(jsonStr, SearchResult.class);

Output :
SearchResult:[value=(value:[group_name=Test, groupid=1, sub_customer=[{"CustomerId":"9488491","CustomerName":"Test","Street":"Test","City":"PENRITH","PostalCode":"275","Country":"Test","Assetcnt":"6"}]])]
 
This was selected as the best answer
VSK98VSK98
Thanks, Sharma it's working perfectly...

Regards,
VSK98