Skip to Content

Scripting Example - Convert Base64 Encoded Value to Text

This REST response Plugin is designed to convert a Base64 enccoded value to text. In the specific example where this script was used, the encoded value was Base64 encoding XML in a REST JSON response.

#r "Newtonsoft.Json.dll"
using System;
using System.Text;
using System.Net.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

string columnToDecode = "file_base64";

string originalContent = await Response.Content.ReadAsStringAsync();
var jobject = JObject.Parse(originalContent);

// Decode the field in the json
byte[] data = Convert.FromBase64String(jobject[columnToDecode].ToString());
string decodedString = Encoding.UTF8.GetString(data);

// Replace the data in json with the decoded value
jobject[columnToDecode] = decodedString;

// Update the response
string newJson = jobject.ToString();
Response.Content = new StringContent(newJson, Encoding.UTF8, "application/json");

Limitations

This Plugin is not expected to work on binary encoding.