1

I want to decode a simple json array using C# that's stored in a GitHub repo to see if it contains a value. I'm using the Newtonsoft json package. I've read this thread: Code for decoding/encoding a modified base64 URL, but I can't seem to implement the solution. I get the following error:

System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters

, but I think there's also something going on with the code.

var value = "somestring";
var encodedTopicTypeURL ="https://api.github.com/repos/org/repo1/contents/sample.json";
string decodedTopicTypeURL;
byte[] buffer = Convert.FromBase64String(encodedTopicTypeURL);
decodedTopicTypeURL = Encoding.UTF8.GetString(buffer);

using (var webClient = new System.Net.WebClient())
{
    var topicTypeJson = webClient.DownloadString(decodedTopicTypeURL);
    JArray validTopicTypes = JArray.Parse(topicTypeJson);
    if (!validTopicTypes.Contains(value))
    {
        Logger.LogError($"Value not found");
     }

Json array:

[
"string1",
"string2",
"string3",
"string4",              
]
Community
  • 1
  • 1
DBS
  • 1,107
  • 1
  • 12
  • 24

2 Answers2

1

https://api.github.com/repos/org/repo1/contents/sample.json is not Base64 encoded string. It's Base64 representation is aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS9yZXBvcy9vcmcvcmVwbzEvY29udGVudHMvc2FtcGxlLmpzb24=

You can download data and when you have it in string variable(I used DownloadData(string url) to retrieve it parse it to JObject, take it Values and look for content inside.

For example:

    static void Main(string[] args)
    {
        string url = @"https://api.github.com/";
        string searchString = "url";
        WebClient wc = new WebClient();
        wc.Headers.Add("user-agent", "Mozilla / 4.0(compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
        try
        {
            byte[] dat = wc.DownloadData(url);
            string data = Encoding.ASCII.GetString(dat);
            var values = JObject.Parse(data).Values();
            bool stringFound = values.Any(x => x.Path.Contains(searchString) ||
                                               x.Value<string>().Contains(searchString)
                                         );
            if(stringFound)
            {
                Console.WriteLine("JSON contains search string");
            }
        }
        catch(Exception e)
        {
            throw;
        }

    }

I'm sure searching through values can be done better (;

ilmash
  • 174
  • 2
  • 12
  • Yes, I see the Base64 representation in the URL for json file in GitHub. When you state it's not a string, what I would like to know is how to properly decode the JSON to access the keys to check for a value. If you have a URL that shows how to do this or a code sample based on my question and code above. – DBS Aug 16 '16 at 17:46
1

Your question doesn't specify why you are trying to decode the URL from Base-64/ Actually encodedTopicTypeURL is just a normal string, why don't you use it directly as below?

var value = "somestring";
var url = "https://api.github.com/repos/org/repo1/contents/sample.json";

using (var webClient = new System.Net.WebClient())
{
    var topicTypeJson = webClient.DownloadString(url);
    JArray validTopicTypes = JArray.Parse(topicTypeJson);
    if (!validTopicTypes.Contains(value))
    {
        Logger.LogError($"Value not found");
     }
}

If you actually need to get the URL off of its base64 representation, then replace the following line

var encodedTopicTypeURL ="https://api.github.com/repos/org/repo1/contents/sample.json";

with this

var encodedTopicTypeURL ="aHR0cHM6Ly9hcGkuZ2l0aHViLmNvbS9yZXBvcy9vcmcvcmVwbzEvY29udGVudHMvc2FtcGxlLmpzb24=";

This way decodedTopicTypeURL will actually have the url value.

You can use this site to play around with Base64 encoding/decoding

Volkan Paksoy
  • 6,727
  • 5
  • 29
  • 40