5

We are calling an External WebService that is returning us the JSON Object. In that Json Object they are sending the PDF as ByteArray(JsonArray) not as Base64 String. So am thinking is there a way to convert that ByteArray/JsonArray to Base64 String that will allow us to make PDF in SFDC end.

Sample Response Json we are getting.(The actual Array is too huge, i have removed some array values just to post it here)

{"FINISHED":[[37,80,68,70,45,49,46,52,10,37,-57,-27,-12,-27,-16,10,51,32,48,32]]}

5

Create a response Object like this

public class Resp {
    public List<List<Integer>> FINISHED { get; set; }
}

then do

Resp r = (Resp) JSON.deserialize(jsonString, Resp.class);

and then finally convert the r.FINISHED into a Blob

List<Integer> values = r.FINISHED.get(0);
String encodedString = String.fromCharArray(values);
Blob someFile = EncodingUtil.base64Decode(encodedString);

(Thanks to @Bachovski for pointing to the fromCharArray function)

  • Converting the bytes to characters in 2 lines: List <Integer> charArr = new List <Integer> {97,65}; String resultString = String.fromCharArray(charArr); – Boris Bachovski Jun 29 '15 at 7:05
  • Tried.. its Giving me System.StringException: Unrecognized base64 character: % – Pranay Jaiswal Jun 29 '15 at 10:01
  • What does encodedString look like? – Willem Mulder Jun 29 '15 at 12:29
  • Encoded String looked like. %PDF-1.4 %ᅦ¥￴¥￰ 3 0 – Pranay Jaiswal Jun 29 '15 at 13:04
  • What happens if you do Blob.valueof(encodedString); ? – Willem Mulder Jun 29 '15 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.