title: Convert blob to string to to create a json object and then change the string back to blob
body: <p>My requirement is to convert the blob in a database field to string to create a json object. I have achieved that. </p>
  
  <p>Now, I need to convert this string back to blob.  I wrote the below code. But, it does not work. In my instance, I have the word document stored as blob. I converted it to string but when I converted the string to blob, the document does not open properly.</p>
  
  <p>Please let me know a way to convert the string back to blob.</p>
  
  <pre><code>DocumentTemplateKey documentTemplateKey = new DocumentTemplateKey();
  documentTemplateKey.documentTemplateID = "XX";
  DocumentTemplateDtls documentTemplateDtls = DocumentTemplateFactory.newInstance().read(documentTemplateKey);
  
  byte[] blobAsBytes = documentTemplateDtls.contents.copyBytes();
  
  ByteArrayOutputStream bos = new ByteArrayOutputStream();
  
  
  bos.write(blobAsBytes, 0, blobAsBytes.length);
  
  String pdfBase64String = 
    org.apache.commons.codec.binary.StringUtils.newStringUtf8(org.apache.
    commons.codec.binary.Base64.encodeBase64(bos.toByteArray()));
  
  OutputStreamWriter out = new OutputStreamWriter(System.out);
  JsonWriter writer = new JsonWriter(out);
  //set indentation for pretty print
  writer.setIndent("\t");
  //start writing
  writer.beginObject(); //{
  writer.name("blob").value(pdfBase64String);
  byte[] stringAsBytes =  org.apache.commons.codec.binary.StringUtils.getBytesUtf8(pdfBase64String);
  
  Blob blob = new Blob(stringAsBytes);
  documentTemplateDtls.contents = blob;
  documentTemplateDtls.documentTemplateID = "XX12";
  documentTemplateDtls.name = "XX12";
  DocumentTemplateFactory.newInstance().insert(documentTemplateDtls);
  writer.endObject();
  
  writer.flush();
  
  //close writer
  writer.close();
  </code></pre>