C# Application to Firebase – Inserting Data (No Base 64 Keys)

Inserting data within Firebase database through use of a C# application with no base 64 keys.

In this post we will be going through the adding of child nodes to the firebase database as well as declaring a parent. This has been a huge request and is an effective way of structuring multiple pieces of data in a schema-free environment. An example of this being implemented in real life would be where users were inserting there personal information such as name, age, address, telephone number and email address. The parent node would be a unique identifier such as an incremental integer and the child nodes would be the entered information for one individual.

Whilst working on this project the aim was to create a project that would insert child nodes in to the firebase database, whilst doing so we were also trying to rid the automatic base-64 keys that would appear within the parent node by default. I had done some research on this and not much was explained as to how this can be removed. However a work around was found, read on to find out more.



We will now move on to the fragments of code, starting with the header files seen below. All of the above header files are those that have been used in previous posts except for the last. The Newtonsoft.Json.Linq which we make use of in order to create the JSON object in C#.

using System;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

In the example being used for this blog we are making use of the current time as variables to be used as data to be inserted into Firebase. The below shows declaring of the current hour, minute and second as strings to be used at a later point.

DateTime date = DateTime.Now;
string hour = date.ToString("HH");
string minutes = date.ToString("mm");
string seconds = date.ToString("ss");

In the next section of code, we the make use of the hour, minute and second strings and place them in to the new string named jsondata which is created in the form of JSON. The string is then initialised as new JObject and then parsed. The parsed JObject is then serialised using JsonConvert made ready for the web request.

DateTime date = DateTime.Now;
string jsondata = @"{" + seconds+ ":{'Hour': "+hour+", 'Minute': " + minutes + ", 'Second': " + seconds + ", }}";
JObject data = JObject.Parse(jsondata);
string json = JsonConvert.SerializeObject(data);

The work around to remove those random base-64 keys as seen in the previous examples we found was by making use of the PATCH function which updates data within Firebase. However by declaring a new parent node when using the PATCH function we see the base keys are replaced with the parent key we set above. You may have noticed that we are making use of the seconds variable twice in the above string once within the child and the other within the parent node resulting in what looks like the below:

Second{
Hour
Minute
Second
}

And so we will have 60 new instances of parent nodes before we see the same nodes being updated over and over. Used only as an example to get the point across, in the real world I would strongly suggest making use of an incremental integer which would act as a true unique identifier.

As seen in all previous posts we then create a web request using the URL of our firebase project to send our local JSON object in to the Firebase database. Be sure you are making use of the PATCH function as the POST will begin generating the base-64 keys as parent nodes.

var request = WebRequest.CreateHttp("https://.firebaseio.com/.json");
request.Method = "PATCH";
request.ContentType = "json";
var buffer = Encoding.UTF8.GetBytes(json);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
var response = request.GetResponse();
json = (new StreamReader(response.GetResponseStream())).ReadToEnd();
string json = JsonConvert.SerializeObject(j_object);

Source code in full is found below, be sure that you have installed the Newtonsoft.Json package to your C# application for this whole project to work.

using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace WindowsFormsApplication
{
public partial class Form1 : Form
{
public Form1()
{

InitializeComponent();
while (true)
{

DateTime date = DateTime.Now;
string hour = date.ToString("HH");
string minutes = date.ToString("mm");
string seconds = date.ToString("ss");

string jsondata = @"{" + seconds+ ":{'Hour': "+hour+",'Minute': " + minutes + ", 'Second': " + seconds + ", }}";
JObject data = JObject.Parse(jsondata); string json = JsonConvert.SerializeObject(data);

var request = WebRequest.CreateHttp(".firebaseio.com/.json");
request.Method = "PATCH";
request.ContentType = "json";
var buffer = Encoding.UTF8.GetBytes(json);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
var response = request.GetResponse();
json = (new StreamReader(response.GetResponseStream())).ReadToEnd();

}
}
}

Leave a Reply