1

I am trying to deserialize base64 file attachments from JSON file using JSON.NET. I can deserialize the data using the following code but I am not sure how to decode the base64 files. Please let me know.

Sample JSON file
-----------------
PostData: {
  "Main":{
     "date1": "14-JAN-2016",
     "summary": "summary test",
     "Details":[{
       "first": "test",
       "last": "last test",
        },
      ]}
     "attachments":[        {
       "title": "Report1.pdf",
       "file_name": "Report1.pdf",
       "file_content_type": "application/pdf",
       "file_format": "binary",
       "file_data": "JVBERi0xLjQNCjUgMCBvYmoNCjw8DQovVHlwZSAvWE9iamVjdA0KL1N1YnR5cGUg
        MSAwIFIvSW5mbyAxMjYgMCBSL0lEWzw0NTQ3QjRFREZBNjk2NDQ4QjhBNUU4MUQw
        QkU4MkVDMD48NDU0N0I0RURGQTY5NjQ0OEI4QTVFODFEMEJFODJFQzA+XSAvUHJl
        diAxMTI3OTI5L1hSZWZTdG0gMTEyNjMxNj4+DQpzdGFydHhyZWYNCjExNDAyNTEN
        CiUlRU9G",
        },
        {
       "title": "src.txt",
       "file_name": "src.txt",
       "file_content_type": "text/plain",
       "file_format": "text",
       "file_data": "bm5gLDJ9NC1DNFkpN20y4tLmM0",
        },
  ]}
}

Program.cs

var json = System.IO.File.ReadAllText("postData.json");
var myJsonObject = JsonConvert.DeserializeObject<PostData>(json);

    public class PostData
    {
        public Main main { get; set; }
    }

    public class Main
    {

        public string date1 { get; set; }
        public string summary { get; set; }
        public List<Details> details { get; set; }
    }

  public class Details
    {
        public string first { get; set; }
        public string last { get; set; }
    }
nav100
  • 2,923
  • 19
  • 51
  • 89
  • Possible duplicate of [Object de-serializing from base64 in C#](https://stackoverflow.com/questions/31339708/object-de-serializing-from-base64-in-c-sharp) – Chris Pickford Jun 01 '17 at 15:14
  • See there https://stackoverflow.com/questions/11743160/how-do-i-encode-and-decode-a-base64-string – tym32167 Jun 01 '17 at 15:15
  • thanks for the reply. How do I save the deserialized content to a local drive depending on the file content type. I don't see this any example on line. – nav100 Jun 01 '17 at 20:15
  • Your JSON is invalid. I tried to clean it up with the result shown here: https://pastebin.com/raw/SPxVx4MH. Can you confirm this is correct? Also, your second `"file_data"`, `"bm5gLDJ9NC1DNFkpN20y4tLmM0"`, is invalid Base64 (wrong string length). Did you manually edit this or is this real Base64 you need to deserialize? – dbc Jun 01 '17 at 20:19

1 Answers1

1

As explained in its Serialization Guide, Json.NET automatically (de)serializes Byte [] arrays as Base64 strings. Thus, after cleaning up your JSON you can upload it to http://json2csharp.com/ to auto-generate classes, then modify the file_data property to be of type byte [] with the result shown below:

public class Detail
{
    public string first { get; set; }
    public string last { get; set; }
}

public class Main
{
    public string date1 { get; set; }
    public string summary { get; set; }
    public List<Detail> Details { get; set; }
}

public class Attachment
{
    public string title { get; set; }
    public string file_name { get; set; }
    public string file_content_type { get; set; }
    public string file_format { get; set; }
    public byte [] file_data { get; set; }
}

public class PostData
{
    public Main Main { get; set; }
    public List<Attachment> attachments { get; set; }
}

Now you can deserialize as follows:

var myJsonObject = JsonConvert.DeserializeObject<RootObject>(json);

Then, to save the deserialized byte arrays to disk, you could do something like the following with File.WriteAllBytes()

foreach (var attachment in myJsonObject.PostData.attachments ?? Enumerable.Empty<Attachment>())
{
    File.WriteAllBytes(attachment.file_name, attachment.file_data);
}
dbc
  • 104,963
  • 20
  • 228
  • 340