Issue in parsing JSON when special characters are included

I have a Json file which has some special character as below:-

{
    "TXT1": "Tést Data",
    "TXT2": "can®."
},

The Special character (é.®) gives me a error BLOB is not a valid UTF-8 string while parsing

The Following is my Apex class:-

List<Document> d = [Select body, bodyLength, ContentType, Url from Document where DeveloperName ='Json_File'];

if(d.size()>0) {
    Blob b =  d[0].body;
    jsonStr = b.toString();  
}

JSONParser parser = JSON.createParser(jsonStr);

Any Help would be greatly appreciated!

Answer

I have tried using same json file document mentioned by you, the only change that i did is iterating the Json String. Please see the below code.

It works.

   try{

        List<Document> docList = [Select Id,Type,body, bodyLength, ContentType, Url from Document WHERE ID='01528000003FqHJAA0'];

        String jsonStr='';
        if(docList.size()>0)
        {
            Blob b =  docList[0].body;
            jsonStr = b.toString();  
        }

        Map<String,Object> mapObj = (Map<String,Object>)JSON.deserializeUntyped(jsonStr);
        for (String key : mapObj.keySet()){
            System.debug('KEY :' + key);
            System.debug('Value :' + mapObj.get(key));
        }

    }catch(Exception exe){
        System.debug('EEROR OCCURED : '+exe.getMessage());
    }

Attribution
Source : Link , Question Author : SFDC Learner , Answer Author : Samuel De Rycke

Leave a Comment