How to import a JSON file format into Couchbase lite database from Android

I’m new with NoSQL concepts and I would like to understand which is the best way to load an JSON file format into an Android App (version 5).

As-is I receive an xml format and store the data received from a remote Server into my Android App, parsing the xml-data with KXml API, and save it into db with SQLite, but the task keeps some time when the file it’s about 20 MB (parsing huge xml file with Kxml Api take some time :frowning: ).

Surfing the net, I have found some interesting concepts about working with JSON file format, instead xml one’s without parsing it, but saving into and NoSQL database like CouchBase lite.

So I would like to ask:

  1. It is possible to load a JSON file format into CouchBase lite database?
  2. It is possible work with documents in my bulk avoiding duplication?
  3. Which is the diff between Couchbase lite vs CouchBase mobile?

Thanks!

Yes. JSON is the standard document format for Couchbase. See the documentation for guidance on getting started with Couchbase Lite and Android: Add Couchbase Lite to your app.

I’m not sure what you mean here.

Couchbase Lite is the embedded NoSQL database. Couchbase Mobile is the entire mobile solution that includes, among other things, the embedded database, a synchronization mechanism and Couchbase Server. See the Couchbase Mobile documentation.

Check out the Android Tutorial for more information.

Hi jkurtz,

I sincerely don’t understand how I could load my JSON file format into couchbaselite database from Android App. Taking a look in the tutorial above, I have found some things about load an database prebuilt-in or an JSONObject but nothing about my purposes.

Until now I have play with SQLite and Relational DB, but never with NOSQL one’s. So for an example if I done a JSON file dump from SQLite I could install my data into CouchBase Lite ?

Snippet code: Install a database
// catalog.zip should unzip in the root folder as a CBL database (i.e catalog.cblite2)
Manager manager = new Manager(new AndroidContext(getApplicationContext()), Manager.DEFAULT_OPTIONS);
Database database = manager.getExistingDatabase(“catalog”);
if (database == null) {
try {
ZipUtils.unzip(getAssets().open(“catalog.zip”), manager.getContext().getFilesDir());
} catch (IOException e) {
e.printStackTrace();
}
database = manager.getExistingDatabase(“catalog”);
}

In the snippet code catalog.zip could be my db export from SQlit that i could import into Couchbase lite db ?

Thanks a lot.

I believe most people use Jackson to parse JSON in Java.

As Jens mentioned, you could use Jackson for mapping JSON to native POJOs. Here is an sample app . Note that the app is for Couchbase Mobile 2.0 DP but the concepts of parsing JSON etc remain the same.

Here is a blog post as well on this topic.

Hi priya.rajagopal,

so if I understand, I could have the same complexity that I have found until now with parsing XML file format, managing POJO’S when I have save them with SQLite.

So Couchbase lite could not resolve my problem, or better does not cut the complexity of reading data (parsing issue with large file ).

Thanks a lot.

I think it would be useful for you to understand what Couchbase Lite does in order to make an informed choice about what you need to use.
Couchbase Lite is an embedded database that you can use as replacement for SQLite
Some high level differences

  • . SQLite defines a schema to model your data and as your data schema evolves, your app may need to deal with complex data migrations . CBL stores data as JSON documents - Its a NoSQL data store and so even if your data structure changes, you do not have to do data migrations.
  • Interfacing with a raw SQLite query interface can be tedious and easy to go wrong. So you would typically use an ORM to interface with from within your app layer. CBL has a native Java interface that your Android app can interact with.
  • If your needs grow to beyond offline only/ local only data store, you can leverage Couchbase Mobile stack which includes Sync Gateway to sync data to from your mobile apps to server. SQLite is just an embedded standalone data store

Parsing JSON data is outside the scope of CBL. There are plenty of platform specific JSON (de)serializers available that is intended to simplify the task of parsing JSON to native data formats.

1 Like

Once data is in Couchbase Lite you don’t have to worry about JSON parsing or generation. All the APIs treat it as POJOs.

I haven’t used Jackson, but I’d be surprised if it took more than two lines of code to parse your JSON file.