[SalesForce] System.JSONException: Unexpected character

I am getting an error when I am trying to parse a JSON response from authorised.net.

System.JSONException: Unexpected character ('' (code 65279 / 0xfeff)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2]

My JSON

{
    "customerPaymentProfileIdList": [],
    "customerShippingAddressIdList": [],
    "validationDirectResponseList": ["1,1,1,(TESTMODE) This transaction has been approved.,000000,P,0,none,Test transaction for ValidateCustomerPaymentProfile.,1.00,CC,auth_only,,RAMESH,TEST,,TEST TEST,test,CA,2222,,,,test123@gmail.com,,,,,,,,,0.00,0.00,0.00,FALSE,none,BF4D71FADF17DA79C3B33ED0D2BA8438,,,,,,,,,,,,,XXXX0015,MasterCard,,,,,,,,,,,,,,,,,"],
    "messages": {
        "resultCode": "Error",
        "message": [{
            "code": "E00039",
            "text": "A duplicate record with ID 1810594140 already exists."
        }]
    }
}

I have tried to parse this JSON by using create parser method.

JSONParser validationparser = JSON.createParser(res.getBody());   
while (validationparser.nextToken() != null) {
if(validationparser.getText()=='validationDirectResponseList'){}              
}

I also try to parse the same JSON by creating a JSON parser in apex class. But still getting the same error. Tried to change the Version from 39 to 30 but nothing worked.

public class JSON2Apex {

public class Messages {
    public String resultCode;
    public List<Message> message;
}

public class Message {
    public String code;
    public String text;
}

public List<CustomerPaymentProfileIdList> customerPaymentProfileIdList;
public List<CustomerPaymentProfileIdList> customerShippingAddressIdList;
public List<String> validationDirectResponseList;
public Messages messages;

public class CustomerPaymentProfileIdList {
}


public static JSON2Apex parse(String json) {
    return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
}

}

Do anyone have any idea to resolve this issue. I totally get stuck with this. Thanks in advance.

Best Answer

The character 65279 is the Unicode Character 'ZERO WIDTH NO-BREAK SPACE'. As this displays as nothing, when pasted into code it can't be seen but is present.

If you are seeing this error in a test case, then I suggest running the JSON text used by the test case through e.g. https://www.textmagic.com/free-tools/unicode-detector and eliminating that/those characters.

If the text is coming directly from an external service, then flag the problem with the service provider.