chronvas/RealmTesting

How to parse nested json object into realm database.

Closed this issue · 2 comments

I am trying to store nested json object into realm database.

Hi @ajit-999
What you describe is out of the scope of this project, but I will try to help as much as I can.

The path I would reccomend you to follow is create your POJOs from that json file, and then try saving it on Realm.
Gson and many other json parsers can handle nested objects. Here is an example of a class named Product containing many Products within.
class Product { @SerializedName("children") private Product[] children; }

Using the gson deserializer could look like this:
public static Product[] deserialize(String nestedJson){ Gson gson = new GsonBuilder().create(); Product[] products = gson.fromJson(nestedJson, Product[].class); return products; }

Take a look at a more detailed guide on handling nested objects with Gson here: https://futurestud.io/tutorials/gson-mapping-of-nested-objects

Now on Realm side, since Realm is relational, I am sure that you can "relate" one nested object to it's parent object, creating basically what you are after. The only requirement would be an id or some other property (or even combined properties of that object) that will be used to uniquely identify each Product object.

I hope that helps