Saving JSON object to Firebase in Android

shj picture shj · Apr 4, 2016 · Viewed 17.7k times · Source

I am new to firebase, and I am trying to put JSON object data into my Firebase. I know how to put data as a class object into Firebase, but I want to put JSON object data.

Is there any way to put JSON object data into Firebase without converting it as a POJO?

Answer

GraSim picture GraSim · Jan 13, 2017

Yes, you can achieve this. Start by converting your json to a string then do the following:

  String jsonString; //set to json string
  Map<String, Object> jsonMap = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, Object>>() {}.getType());

I am using the "updateChildren" method because I want the JSON object added directly to the root of my child object

  firebaseDatabaseRef.child("users").child(uid).updateChildren(jsonMap);

If you don't care or you would like to set a new node, use

 firebaseDatabaseRef.child("users").child(uid).child({your new node}).setValue(jsonMap);