12

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?

simhumileco
  • 31,877
  • 16
  • 137
  • 115
inin
  • 372
  • 1
  • 4
  • 17
  • i think without POJO it is very difficult to store data into firebase ..... – sushildlh Apr 04 '16 at 06:17
  • Firebase saves data as a json, but why can't I save data as json? – inin Apr 04 '16 at 06:21
  • Can you please post a json data you want to store in firebase? – Reaz Murshed Apr 04 '16 at 06:24
  • @shj see my answer you can save what ever json data you want in firebase using import json option in firebase – Kathi Apr 04 '16 at 06:26
  • Java has no native format to represent JSON. In what format do you have the JSON data? If it's a `Map` then you can just pass that into `ref.setValue()` the same as a POJO (as Emin answered). – Frank van Puffelen Apr 04 '16 at 14:43
  • I think this is the [same question](http://stackoverflow.com/questions/38270153/pushing-json-file-to-firebase-database-android/39540908#39540908), I used its answers. – josue.0 Sep 16 '16 at 22:29

5 Answers5

10

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);
GraSim
  • 3,830
  • 1
  • 29
  • 35
  • 1
    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. – GraSim Jan 13 '17 at 22:41
6

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);
kinny94
  • 376
  • 1
  • 5
  • 22
Emin Ayar
  • 1,104
  • 9
  • 13
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);
Community
  • 1
  • 1
Vincent de Lagabbe
  • 4,964
  • 3
  • 31
  • 38
2

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!

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
2

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)
AndyGeek
  • 99
  • 1
  • 7