Realm-js: Storing JSON

Created on 4 Sep 2015  路  10Comments  路  Source: realm/realm-js

React uses AsyncStorage and we can just write an object (json) to disk.

I'm thinking I want to do that in Realm. That is, any given table would have a "document" column and I'd put a bunch of JSON in there. The other columns would only be things I wanted to filter or sort by.

Is that crazy? Here are some of the benefits

  • I can actually have a null value :-)
  • I can do simple nesting - which is a easier and more reliable than making object graphs in the db
  • less migrations
  • more or less can cache what comes from the server

The downsides are probably something like the downsides to NoSQL in general

  • there are still "migrations" - it's just internal versioning stuff

If it is a not-crazy idea, would I just store a String of the json or is there some more native way to do it?
Is there a string size limit? I see something about 16 MB in the docs for a property.

Most helpful comment

It's not a crazy idea. We have been planning on supporting a generalized dictionary type with query support (where values can be any supported type) across all the bindings but it has been lower on the list of priorities.

If you want support for any valid JSON, then you would also need support for arrays, which goes beyond what we have previously discussed. The easiest way to make this work would be to store your data it in a string property, and to add a wrapper property to get/set your json object which automatically converts to/from the stored string property. Something like this:

function MyObject() {}
MyObject.prototype.schema = {
  name: 'MyObject',
  properties: [
    {name: 'jsonString', type: RealmType.String},
  ]
};
Object.defineProperty(MyObject.prototype, "json", {
  get: function()    { return JSON.parse(this.jsonString); },
  set: function(val) { this.jsonString = JSON.stringify(val); }
});

All 10 comments

It's not a crazy idea. We have been planning on supporting a generalized dictionary type with query support (where values can be any supported type) across all the bindings but it has been lower on the list of priorities.

If you want support for any valid JSON, then you would also need support for arrays, which goes beyond what we have previously discussed. The easiest way to make this work would be to store your data it in a string property, and to add a wrapper property to get/set your json object which automatically converts to/from the stored string property. Something like this:

function MyObject() {}
MyObject.prototype.schema = {
  name: 'MyObject',
  properties: [
    {name: 'jsonString', type: RealmType.String},
  ]
};
Object.defineProperty(MyObject.prototype, "json", {
  get: function()    { return JSON.parse(this.jsonString); },
  set: function(val) { this.jsonString = JSON.stringify(val); }
});

Yeah, that's what I did last night.
Working well.

So what about the second problem

Is there a string size limit? I see something about 16 MB in the docs for a property.

will the string size create an issue.

@ismdcf if you want to store larger blocks of data you will need to break it up into smaller pieces or store it outside of Realm.

@alazier Is there a new solution for this? I also want to store a JSON value that will constantly change and contain arrays and such. Is using string still the best method?

@brunobraga95 no new solution as of yet.

@alazier does that mean everytime I need to do a write/query I will have to retrieve and save the whole data?

Has nothing been done for this? Why was this closed? The solution posted by @alazier doesn't appear to work anymore.

I have nested JSON objects coming from an API; some of those fields are simple JSON. I have no need to search the JSON fields, I merely want to read and write it naturally without needing to serialize/deserialize manually.

I have JSON comming from API it contains the fields whose data type is String and Object.
How to store this nested JSON in realm DB column

@Karthikpala This doesn't seem closed, could please expain the fix

Was this page helpful?
0 / 5 - 0 ratings