Connected successfully



C# Application to Firebase Database - Inserting Parent & Child Nodes (Removal of Base-64 Keys)






Welcome back to a new post here at Scriptopia, today 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. Where the parent node would be a unique identifier such as an incremental integer. Where the child nodes would be the entered information for one individual.

Before continuing to read onwards I would strongly advise to read on the previous articles working through the basics of C# and Firebase which can be found below:
Inserting data in to the Firebase database
Updating data in the Firebase database
Deleting data in Firebase database
Retrieving all data from the Firebase database
Retrieving Individual parent and child nodes from the Firebase database

Again as in all posts I must declare that no authentication is used, there are no current rules within the Firebase database.
If however you are wanting to have some form of authentication added to your project check out the below
Authenticating Your C# Application with the Firebase Database


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#



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.



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.





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:



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.



If you are not yet familiar with the above code it is suggest you read up on some of the previous articles a list of which can be found below:
Inserting data in to the Firebase database
Updating data in the Firebase database
Deleting data in Firebase database
Retrieving all data from the Firebase database
Retrieving Individual parent and child nodes from the Firebase database



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. Also show your support by liking, subscribing to the YouTub channel and sharing all the relevant links.