App42 back-end platform provides an easy interface to save your app data in the form of JSON objects using our Storage API. Your JSON app data is saved inside a given collection and database. Here collection is like a table and your JSON object represents a row inside that collection. Your App can have only a single db and as many collections inside it.
Once your JSON object is saved in the App42 Cloud, you can query your stored objects using query interface. See Querying App Data for more details.
To save your JSON app data, you have to pass your app database name, collection name and JSON object that you want to save. If you are saving a JSON object for the first time, a new database and collection is created automatically. You have to use the same database name for further operations because only one database is allowed per app. However you can have multiple collections inside your app database.
Once your JSON object is saved, you will get a unique identifier objectId of the stored json object. ObjectId can be used to refer to that JSON object stored in App42 cloud.
Here is the snippet for saving the JSON Object -
String dbName = "test"; String collectionName = "foo"; String employeeJSON = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; /* Below snippet will save JSON object in App42 Cloud */ storageService.insertJSONDocument(dbName,collectionName,employeeJSON,new App42CallBack() { public void onSuccess(Object response) { Storage storage = (Storage )response; ArrayList<Storage.JSONDocument> jsonDocList = storage.getJsonDocList(); for(int i=0;i<jsonDocList.size();i++) { System.out.println("objectId is " + jsonDocList.get(i).getDocId()); //Above line will return object id of saved JSON object System.out.println("CreatedAt is " + jsonDocList.get(i).getCreatedAt()); System.out.println("UpdatedAtis " + jsonDocList.get(i).getUpdatedAt()); System.out.println("Jsondoc is " + jsonDocList.get(i).getJsonDoc()); } } public void onException(Exception ex) { System.out.println("Exception Message"+ex.getMessage()); } });NSString *dbName = @"test"; NSString *collectionName = @"foo"; NSString *json = @{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}; Storage *storage = [storageService insertJSONDocument:dbName collectionName:collectionName json:json]; /* Above snippet will save JSON object in App42 Cloud */ NSMutableArray *jsonDocArray = storage.jsonDocArray; for(JSONDocument *jsonDoc in jsonDocList) // This will have only single object stored above { NSLog(@"objectId is = %@ " , jsonDoc.docId); //Above snippet will return object id of saved JSON object NSLog(@"Created At = %@ " , jsonDoc.createdAt); }public class Callback : App42Callback { String dbName = "test"; String collectionName = "foo"; String employeeJSON = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; storageService.InsertJSONDocument(dbName,collectionName,employeeJSON,requestCallback); void App42Callback.OnSuccess(Object response) { // Save JSON Object Success Storage storage = (Storage) response; IList<Storage.JSONDocument> JsonDocList = storage.GetJsonDocList(); // This will have only single object stored above for(int i=0;i <JsonDocList.Count;i++) { Console.WriteLine("objectId is " + JsonDocList[i].GetDocId()); //Above snippet will return object id of saved JSON object Console.WriteLine("Created At " + JsonDocList[i].GetCreatedAt()); } }String dbName = "test"; String collectionName = "foo"; String employeeJSON = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; Storage storage = storageService.insertJSONDocument(dbName,collectionName,employeeJSON); /* Above snippet will save JSON object in App42 Cloud */ Vector JsonDocList = storage.getJsonDocList(); // This will have only single object stored above for(int i=0;i < JsonDocList.size();i++) { Storage.JSONDocument jsonDoc = (Storage.JSONDocument)JsonDocList.elementAt(i); System.out.println("objectId is " + jsonDoc.getDocId()); //Above snippet will return object id of saved JSON object System.out.println("Created At " + jsonDoc.getCreatedAt()); }var dbName = "test", collectionName = "foo", employeeJSON = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; var result ; storageService.insertJSONDocument(dbName, collectionName, employeeJSON,{ success: function(object) { var storageObj = JSON.parse(object); result = storageObj.app42.response.storage; console.log("dbName is " + result.dbName) console.log("objectId is " + result.jsonDoc._id.$oid) }, error: function(error) { } });String dbName = "test"; String collectionName = "foo"; String employeeJSON = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; Storage storage = storageService.insertJSONDocument(dbName,collectionName,employeeJSON); /* Above snippet will save JSON object in App42 Cloud */ ArrayList<Storage.JSONDocument>jsonDocList = storage.getJsonDocList(); // This will have only single object stored above for(Storage.JSONDocument jsonDoc : jsonDocList) { System.out.println("Object Id is " + jsonDoc.getDocId()); //Above snippet will return object id of saved JSON object System.out.println("Created At " + jsonDoc.getCreatedAt()); }String dbName = "test"; String collectionName = "foo"; String employeeJSON = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; storageService.InsertJSONDocument(dbName, collectionName, employeeJSON, new UnityCallBack()); public class UnityCallBack : App42CallBack { public void OnSuccess(object response) { Storage storage = (Storage) response; IList<Storage.JSONDocument> jsonDocList = storage.GetJsonDocList(); for(int i=0;i <jsonDocList.Count;i++) { App42Log.Console("objectId is " + jsonDocList[i].GetDocId()); App42Log.Console("Created At " + jsonDocList[i].GetCreatedAt()); } } public void OnException(Exception e) { App42Log.Console("Exception : " + e); } }String dbName = "test"; String collectionName = "foo"; String json = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; Storage storage = storageService.InsertJSONDocument(dbName,collectionName,json); /* Above snippet will save JSON object in App42 Cloud */ IList<Storage.JSONDocument> JsonDocList = storage.GetJsonDocList(); // This will have only single object stored above for(int i=0;i <JsonDocList.Count;i++) { Console.WriteLine("objectId is " + JsonDocList[i].GetDocId()); //Above snippet will return object id of saved JSON object Console.WriteLine("Created At " + JsonDocList[i].GetCreatedAt()); }$dbName = "test"; $collectionName = "foo"; $json = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; $storage = $storageService->insertJSONDocument($dbName, $collectionName, $json); /* Above snippet will save JSON object in App42 Cloud */ $jsonDocList = $storage->getJsonDocList(); // This will have only single object stored above foreach( $jsonDocList as $jsonDoc ) { print_r("objectId is" . $jsonDoc->getDocId()); //Above snippet will return object id of saved JSON object }dbName = "test"; collectionName = "foo"; json = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; storage = storageService.insert_json_document(dbName,collectionName,json); /* Above snippet will save JSON object in App42 Cloud */ jsonDocList = Array.new(); jsonDocList = storage.jsonDocList(); // This will have only single object stored above for jsonDoc in jsonDocList do puts "objectId is #{jsonDoc.docId}"; //Above snippet will return object id of saved JSON object endvar dbName:String = "test"; var collectionName:String = "foo"; var jsonObj:Object = new Object(); jsonObj.key = "value"; storageService.insertJSONDocument(dbName,collectionName,jsonObj, new callback()); public class callback implements App42CallBack { public function onException(excption:App42Exception):void { trace("Exception Message"); } public function onSuccess(response:Object):void { var storage:Storage = Storage(response); } }N/A
If you want to tag the geo location along with your JSON Object to be saved, you can set lat long along with the JSON Object as shown in the below snippet. After that you can apply a geo query on your saved JSON object to fetch nearby JSON objects from any given location (See How to Fetch JSON Object Using Geo Query). If your app is based on Geo Spatial data, this might be helpful for you.
String dbName = "test"; String collectionName = "foo"; GeoTag gp = new GeoTag(); gp.setLat(new BigDecimal(-73.99171)); gp.setLng(new BigDecimal(40.738868)); storageService.setGeoTag(gp); String employeeJSON = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; /* Below snippet will save JSON object in App42 Cloud along with Geo Tag*/ storageService.insertJSONDocument(dbName,collectionName,employeeJSON,new App42CallBack() { public void onSuccess(Object response) { Storage storage = (Storage )response; ArrayList<Storage.JSONDocument> jsonDocList = storage.getJsonDocList(); for(int i=0;i<jsonDocList.size();i++) { System.out.println("objectId is " + jsonDocList.get(i).getDocId()); //Above line will return object id of saved JSON object System.out.println("CreatedAt is " + jsonDocList.get(i).getCreatedAt()); System.out.println("UpdatedAtis " + jsonDocList.get(i).getUpdatedAt()); System.out.println("Jsondoc is " + jsonDocList.get(i).getJsonDoc()); } } public void onException(Exception ex) { System.out.println("Exception Message"+ex.getMessage()); } });NSString *dbName = @"test"; NSString *collectionName = @"foo"; GeoTag *geoTag = [[GeoTag alloc] initWithLatitude:20.2 andLongitude:50.5]; storageService.geoTag = [geoTag toString]; NSString *json = @{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}; Storage *storage = [storageService insertJSONDocument:dbName collectionName:collectionName json:json]; /* Above snippet will save JSON object in App42 Cloud with Geo Tag */ NSMutableArray *jsonDocArray = storage.jsonDocArray; for(JSONDocument *jsonDoc in jsonDocList) // This will have only single object stored above { NSLog(@"objectId is = %@ " , jsonDoc.docId); //Above snippet will return object id of saved JSON object NSLog(@"Created At = %@ " , jsonDoc.createdAt); }public class Callback : App42Callback { String dbName = "test"; String collectionName = "foo"; GeoTag gp = new GeoTag(); gp.SetLat(-73.99171); gp.SetLng(40.738868); storageService.SetGeoTag(gp); String employeeJSON = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; /* Below snippet will save JSON object in App42 Cloud along with Geo Tag*/ storageService.InsertJSONDocument(dbName,collectionName,employeeJSON,requestCallback); void App42Callback.OnSuccess(Object response) { // Save JSON Object Success Storage storage = (Storage) response; IList<Storage.JSONDocument> JsonDocList = storage.GetJsonDocList(); // This will have only single object stored above for(int i=0;i <JsonDocList.Count;i++) { Console.WriteLine("objectId is " + JsonDocList[i].GetDocId()); //Above snippet will return object id of saved JSON object Console.WriteLine("Created At " + JsonDocList[i].GetCreatedAt()); } }String dbName = "test"; String collectionName = "foo"; GeoTag gp = new GeoTag(); gp.setLat(new BigDecimal(-73.99171)); gp.setLng(new BigDecimal(40.738868)); storageService.setGeoTag(gp); String employeeJSON = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; Storage storage = storageService.insertJSONDocument(dbName,collectionName,employeeJSON); /* Above snippet will save JSON object in App42 Cloud along with Geo Tag*/ Vector JsonDocList = storage.getJsonDocList(); // This will have only single object stored above for(int i=0;i < JsonDocList.size();i++) { Storage.JSONDocument jsonDoc = (Storage.JSONDocument)JsonDocList.elementAt(i); System.out.println("objectId is " + jsonDoc.getDocId()); //Above snippet will return object id of saved JSON object System.out.println("Created At " + jsonDoc.getCreatedAt()); }var dbName = "test", collectionName = "foo", var gp = new GeoTag(); gp.setLat(-73.1234) gp.setLng(-26.1234) storageService.setGeoTag(gp) employeeJSON = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; var result ; /* Below snippet will save JSON object in App42 Cloud along with Geo Tag*/ storageService.insertJSONDocument(dbName, collectionName, employeeJSON,{ success: function(object) { var storageObj = JSON.parse(object); result = storageObj.app42.response.storage; console.log("dbName is " + result.dbName) console.log("objectId is " + result.jsonDoc._id.$oid) }, error: function(error) { } });String dbName = "test"; String collectionName = "foo"; GeoTag gp = new GeoTag(); gp.setLat(new BigDecimal(-73.99171)); gp.setLng(new BigDecimal(40.738868)); storageService.setGeoTag(gp); String employeeJSON = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; Storage storage = storageService.insertJSONDocument(dbName,collectionName,employeeJSON); /* Above snippet will save JSON object in App42 Cloud along with Geo Tag*/ ArrayList<Storage.JSONDocument>jsonDocList = storage.getJsonDocList(); // This will have only single object stored above for(Storage.JSONDocument jsonDoc : jsonDocList) { System.out.println("Object Id is " + jsonDoc.getDocId()); //Above snippet will return object id of saved JSON object System.out.println("Created At " + jsonDoc.getCreatedAt()); }String dbName = "test"; String collectionName = "foo"; GeoTag gp = new GeoTag(); gp.SetLat(-73.99171); gp.SetLng(40.738868); storageService.SetGeoTag(gp); String employeeJSON = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; storageService.InsertJSONDocument(dbName, collectionName, employeeJSON, new UnityCallBack()); /* Above snippet will save JSON object in App42 Cloud along with Geo Tag*/ public class UnityCallBack : App42CallBack { public void OnSuccess(object response) { Storage storage = (Storage) response; IList<Storage.JSONDocument> jsonDocList = storage.GetJsonDocList(); for(int i=0;i <jsonDocList.Count;i++) { App42Log.Console("objectId is " + jsonDocList[i].GetDocId()); App42Log.Console("Created At " + jsonDocList[i].GetCreatedAt()); } } public void OnException(Exception e) { App42Log.Console("Exception : " + e); } }String dbName = "test"; String collectionName = "foo"; GeoTag gp = new GeoTag(); gp.SetLat(-73.99171); gp.SetLng(40.738868); storageService.SetGeoTag(gp); String json = "{\"name\":\"Nick\",\"age\":30,\"phone\":\"xxx-xxx-xxx\"}"; Storage storage = storageService.InsertJSONDocument(dbName,collectionName,json); /* Above snippet will save JSON object in App42 Cloud along with Geo Tag*/ IList<Storage.JSONDocument> JsonDocList = storage.GetJsonDocList(); // This will have only single object stored above for(int i=0;i <JsonDocList.Count;i++) { Console.WriteLine("objectId is " + JsonDocList[i].GetDocId()); //Above snippet will return object id of saved JSON object Console.WriteLine("Created At " + JsonDocList[i].GetCreatedAt()); }NANAvar dbName:String = "test"; var collectionName:String = "foo"; var gp:GeoTag = new GeoTag(); gp.setLat(-73); gp.setLng(48); storageService.setGeoTag(gp); var jsonObj:Object = new Object(); jsonObj.key = "value"; storageService.insertJSONDocument(dbName,collectionName,jsonObj, new callback()); /* Above snippet will save JSON object in App42 Cloud along with Geo Tag*/ public class callback implements App42CallBack { public function onException(excption:App42Exception):void { trace("Exception Message"); } public function onSuccess(response:Object):void { var storage:Storage = Storage(response); } }N/A
You can save all date types supported by JSON however if your app data contains date field to be saved, it should be saved in UTC formatted String. If you save it in the normal format you will not be able to apply date comparison queries while searching for documents. You can create UTC formatted String from Date object using our helper method and can save it further as shown below. See How to Search JSON Object Using Date Query
String dbName = "test"; String collectionName = "foo"; java.util.Date dateTobeSaved = new Date(); // Date that you want to save String utcDateFormat = com.shephertz.app42.paas.sdk.android.util.Util.getUTCFormattedTimestamp(dateTobeSaved); JSONObject jsonData = new JSONObject(); jsonData.put("name", "Nick"); jsonData.put("DOB", utcDateFormat); // Inserting json document. storage.insertJSONDocument(dbName, collectionName, jsonData,new App42CallBack() { public void onSuccess(Object response) { Storage storage = (Storage )response; ArrayList<Storage.JSONDocument> jsonDocList = storage.getJsonDocList(); for(int i=0;i<jsonDocList.size();i++) { System.out.println("objectId is " + jsonDocList.get(i).getDocId()); //Above line will return object id of saved JSON object System.out.println("CreatedAt is " + jsonDocList.get(i).getCreatedAt()); System.out.println("UpdatedAtis " + jsonDocList.get(i).getUpdatedAt()); System.out.println("Jsondoc is " + jsonDocList.get(i).getJsonDoc()); } } public void onException(Exception ex) { System.out.println("Exception Message"+ex.getMessage()); } });NSString *dbName = "test"; NSString *collectionName = "foo"; NSDate *dateTobeSaved= [NSDate date]; // Date on which you want to query NSString *utcDateFormat = [Utils getUTCTimeFormattedStamp:dateTobeSaved]; NSDictionary *dataDict = [NSDictionary dictionaryWithObjectsAndKeys:@"Nick",@"name",utcDateFormat,@"DOB", nil]; // Inserting json document. Storage *storageObj = [storageObj insertJSONDocument:dbName collectionName:collectionName json:[dataDict JSONRepresentation]];String dbName = "queryTest"; String collectionName = "foo"; DateTime dateTobeSaved = DateTime.Now; // Date that you want to save String utcDateFormat = com.shephertz.app42.paas.sdk.windows.util.Util.GetUTCFormattedTimestamp(dateTobeSaved); JObject jsonData = new JObject(); jsonData.Add("name", "Nick"); jsonData.Add("DOB", utcDateFormat); // Inserting json document. storageService.InsertJSONDocument(dbName, collectionName, jsonData, this); public class Callback : App42Callback { public void OnException(App42Exception exception) { Console.WriteLine("Exception Message : " + exception); } public void OnSuccess(Object response) { Storage storage = (Storage) response; IList<Storage.JSONDocument> jsonDocList = storage.GetJsonDocList(); for(int i=0;i <jsonDocList.Count;i++) { Console.WriteLine("DocId is : " + jsonDocList[i].GetDocId()); Console.WriteLine("JSONDoc is : " + jsonDocList[i].GetJsonDoc()); } } }String dbName = "test"; String collectionName = "foo"; java.util.Date dateTobeSaved = new Date(); // Date that you want to save String utcDateFormat = com.shephertz.app42.paas.sdk.jme.util.Util.getUTCFormattedTimestamp(dateTobeSaved); JSONObject jsonData = new JSONObject(); jsonData.put("name", "Nick"); jsonData.put("DOB", utcDateFormat); // Inserting json document. Storage storageObj = storage.insertJSONDocument(dbName, collectionName, jsonData);var dbName = "test"; var collectionName = "foo"; var jsonData = {} jsonData.name = "Nick" jsonData.DOB = new Date().toJSON() storageService.insertJSONDocument(dbName, collectionName, jsonData,{ success: function(object) { console.log(object) }, error: function(error) { console.log(error) } });String dbName = "test"; String collectionName = "foo"; java.util.Date dateTobeSaved = new Date(); // Date that you want to save String utcDateFormat = com.shephertz.app42.paas.sdk.java.util.Util.getUTCFormattedTimestamp(dateTobeSaved); JSONObject jsonData = new JSONObject(); jsonData.put("name", "Nick"); jsonData.put("DOB", utcDateFormat); // Inserting json document. Storage storageObj = storage.insertJSONDocument(dbName, collectionName, jsonData);String dbName = "queryTest"; String collectionName = "foo"; DateTime dateTobeSaved = DateTime.Now; // Date that you want to save String utcDateFormat = com.shephertz.app42.paas.sdk.csharp.util.Util.GetUTCFormattedTimestamp(dateTobeSaved); JSONClass jsonData = new JSONClass(); jsonData.Add("name", "Nick"); jsonData.Add("DOB", utcDateFormat); storageService.InsertJSONDocument(dbName, collectionName, jsonData, new UnityCallBack()); public class UnityCallBack : App42CallBack { public void OnSuccess(object response) { Storage storage = (Storage) response; IList<Storage.JSONDocument> jsonDocList = storage.GetJsonDocList(); for(int i=0;i <jsonDocList.Count;i++) { App42Log.Console("objectId is " + jsonDocList[i].GetDocId()); } } public void OnException(Exception e) { App42Log.Console("Exception : " + e); } }String dbName = "queryTest"; String collectionName = "foo"; DateTime dateTobeSaved = DateTime.Now; // Date that you want to save String utcDateFormat = com.shephertz.app42.paas.sdk.csharp.util.Util.GetUTCFormattedTimestamp(dateTobeSaved); JSONClass jsonData = new JSONClass(); jsonData.Add("name", "Nick"); jsonData.Add("DOB", utcDateFormat); // Inserting json document. Storage storageObj = storageService.InsertJSONDocument(dbName, collectionName, jsonData);TBDTBDvar dbName:String = "test"; var collectionName:String = "foo"; var dateTobeSaved:Date = new Date(); var utcDateFormat:String= com.shephertz.app42.paas.sdk.as3.util.Util.getUTCTimestampFromDate(dateTobeSaved); var jsonData:Object = new Object(); jsonData.name = "Nick"; jsonData.DOB = utcDateFormat; storageService.insertJSONDocument(dbName,collectionName,jsonData, new callback()); public class callback implements App42CallBack { public function onException(excption:App42Exception):void { trace("Exception Message"); } public function onSuccess(response:Object):void { var storage:Storage = Storage(response); trace("dbName is : " + storage.getDbName()); trace("collectionName is : " + storage.getCollectionName()); var jsonDoc:JSONDocument = new JSONDocument(); for(var i:int = 0; i<storage.getJsonDocList().length;i++) { jsonDoc = JSONDocument(storage.getJsonDocList()[i]); trace("objectId is : " + jsonDoc.getDocId()); } } }N/A
Similarly you can also save user data in JSON format. For example if you want to save user favorite food dish, you can create a JSON object like {“username”:”Nick”, “favouriteFood”:”Whole Maine lobster”} and save it in given database and collection.