15

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!

5
  • 5
    You need to ensure that the text file that is uploaded to the Document is in UTF-8 format. Google to find editors and tools that are good at that. Then the Apex code should just work.
    – Keith C
    Commented Aug 22, 2014 at 9:11
  • when i save the file as UTF-8 format, it gives me the error as System.JSONException: Unexpected character ('' (code 65279 / 0xfeff)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2] Commented Aug 22, 2014 at 11:34
  • 12
    Sounds like you've got the UTF-8 encoding corrected, but you have a "ZERO WIDTH NO-BREAK SPACE" character in the text. While this doesn't display so makes the JSON appear OK, it is not syntactically valid in JSON. It appears to be at the beginning of your file so you could remove it by deleting the first few characters and retyping them (and then re-uploading the file). But there may also be instances further down in the file.
    – Keith C
    Commented Aug 22, 2014 at 12:05
  • Yes, UTF-8 without BOM worked for me. Thanks Keith for your helpful response Commented Aug 28, 2014 at 10:11
  • 3
    @KeithC You should put this into an answer, would be helpful for others Commented Jun 21, 2016 at 9:49

3 Answers 3

1

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());
    }
0

Have you tried escape sequence \u with hex value corresponding to special char ?

\u followed by four-hex-digits

1
  • Can you share some code snippet...? Commented Jul 28, 2016 at 13:47
0

Refer below link for more help for escape special characters

https://stackoverflow.com/questions/19176024/how-to-escape-special-characters-in-building-a-json-string

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .