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]]}

1 Answer 1

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)

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.