Saving JSON object to Firebase in Android

19,335

Solution 1

As you've tagged android in your question, so I guess you want to put some data in your firebase database from the Android client.

You've some JSON data and you want it to store in firebase database right? This is the step by step procedure to get it done.

I don't really know why are you trying to avoid POJO. It makes the total operation more simplified. Now let us assume you've a JSON string. You need to map your JSON to a java class to put it directly in the firebase. Firebase stores the data in a JSON-like format which is easily understandable.

I use Gson to map a JSON string to a specific java object. Its the easiest method I've found so far needs few lines of coding.

Gson gson = new Gson();
YourPOJO myPOJO = gson.fromJson(jsonString, YourPOJO.class);

Now you've mapped your JSON string to myPOJO object successfully. You're ready to put this object in your Firebase database now.

Lets take the reference to your Firebase database and then call setValue in that specific node where you want put the data.

Firebase ref = new Firebase(YOUR_FIREBASE_REF);
ref.setValue(myPOJO);

Simple!

Solution 2

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);

Solution 3

It is so simple to also save JSON value on firebase. Let's say you have called your firebase reference

Firebase ref = new Firebase("https://docs-examples.firebaseio.com/android/saving-data/fireblog");

Then you will pass your data with Map objects. Map keeps values with keys. In your case you will have a String key and JSONObject value.

Firebase userRef = ref.child("user");
Map<String, JSONObject> userMap= new HashMap<String, JSONObject>();
JSONObject tempObject = new JSONObject();

try{
   tempObject.put("birthday","1992");
   tempObject.put("fullName","Emin AYAR");
}catch(Exception e){
   e.printStackTrace();
}
userMap.put("myUser", tempObject);

userRef .setValue(userMap);

Solution 4

As suggested here: Convert a JSON String to a HashMap

you can easily convert your json object/string to a Map<String, Object> (you don't even need to create a POJO):

Firebase ref = new Firebase(YOUR_FIREBASE_REF);
Map<String, Object> map = new Gson().fromJson(jsonString, new TypeToken<HashMap<String, Object>>() {}.getType());
ref.setValue(map);

Solution 5

Solution In Kotlin because Auto Conversion convert it with compile time error saying : "Not enough information to infer parameter T" with Kotlin and Android

val jsonMap = Gson().fromJson<Any>(stringJson, object : TypeToken<HashMap<String, Any>>() {}.type)
Share:
19,335
inin
Author by

inin

Updated on June 15, 2022

Comments

  • inin
    inin about 2 years

    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?

  • GraSim
    GraSim over 7 years
    Just noticed another poster had the same method, I will leave my answer up bcos of the additional info on how to set the JSON object in the Firebase DB.
  • Satyam Gondhale
    Satyam Gondhale over 5 years
    is it add value in Firebase database having key name myUser ?