can I save big JSON object directly to sqlite databse - android

how can I save JSON object into sqlite database. Which is the best data type to save JSON object in sqlite? Now I am using String data type to save JSON data.

Just save it in the TEXT/VARCHAR format. JSON is nothing but textual representation of Data.
Write to DB
String data = jsonObject.toString();
//save data to db
Read from DB
String data = //read from db
JSONObject jsonObject = new JSONObject(data);

There is no specific type for JSONObject.
All you can do is make String from your JSONObject and write it to database using:
String myString = myJsonObject.toString();
//make DB insert here
Then when needed, just read it:
String myString = /* read from DB here */
JSONObject myjsonObject = new JSONObject(myString);
Type for column should be TEXT - SQLite doc

Related

How to extract objects from json

I have a scenario where I need only 1 objects out of the entire json.
{"id":"1","first_name":"Steve","last_name":"Holt","user_type":"Teacher","user_key_area":"Math"}
In above I want to extract user_type.
How will I do?
You can use Google GSON to parse the json response and map it to the model directly . Here is the tutorial for the same TUTORIAL
Use this to extract the user_type from the json, pass the your json response in place of response variable.
JsonObject object =new JsonObject(response);
String user_type = object.getString("user_type");
You are getting this json in response you can get 1 object like this:
String value = response.getString("Key Name");
Get Response and that create JsonObject
JsonObject jsonObject =new JsonObject(res);
Create String object and pass string parameter "user_type"
String userType = jsonObject.getString("user_type");

Extract JSON data from Json Feed

I am trying to extract data using json feed. But I get NullPointerexception due to there is no JSONObject.
I am trying to extract following data.
{"postcode":"HA0 4TL","geo": {"lat":51.55135528090932,"lng":-0.29906534560684617,"easting":518029.0,"northing":184978.0,"geohash":"http://geohash.org/gcpv30whrm0s"},"administrative":{"council":{"title":"Brent","uri":"http://statistics.data.gov.uk/id/statistical-geography/E09000005","code":"E09000005"},"ward":{"title":"Wembley Central","uri":"http://statistics.data.gov.uk/id/statistical-geography/E05000104","code":"E05000104"},"constituency":{"title":"Brent North","uri":"http://statistics.data.gov.uk/id/statistical-geography/E14000592","code":"E14000592"}}}
I can create JSONObject as follow.
String in;
JSONObject reader = new JSONObject(in);
But how can I get postcode?
Following code returns NullPointerException.
JSONObject postCode = reader.getJSONObject("postcode");
You should use
String postCode = reader.getString("postcode");

How to store JSON object in SQLite database

how do I store a JSON Object in an SQLite database? What is the correct way?
one place is the blob type column. if i can convert the JSON object into byte array and use Fileoutputstream
the other idea is to store in a text column as a String
import org.json.JSONObject;
JSONObject jsonObject;
public void createJSONObject(Fields fields) {
jsonObject = new JSONObject();
try {
jsonObject.put("storedValue1", fields.storedValue1);
jsonObject.put("storedValue2", fields.storedValue2);
jsonObject.put("storedValue3", fields.storedValue3);
jsonObject.put("storedValue4", fields.storedValue4);
jsonObject.put("storedValue5", fields.storedValue5);
jsonObject.put("storedValue6", fields.storedValue6);
} catch (JSONException e) {
e.printStackTrace();
}
}
Convert JSONObject into String and save as TEXT/ VARCHAR. While retrieving the same column convert the String into JSONObject.
For example
Write into DB
String stringToBeInserted = jsonObject.toString();
//and insert this string into DB
Read from DB
String json = Read_column_value_logic_here
JSONObject jsonObject = new JSONObject(json);
An alternative could be to use the new JSON extension for SQLite. I've only just come across this myself: https://www.sqlite.org/json1.html This would allow you to perform a certain level of querying the stored JSON. If you used VARCHAR or TEXT to store a JSON string you would have no ability to query it. This is a great article showing its usage (in python) http://charlesleifer.com/blog/using-the-sqlite-json1-and-fts5-extensions-with-python/
There is no data types for that.. You need to store it as VARCHAR or TEXT only.. jsonObject.toString();
https://github.com/requery/sqlite-android allows you to query JSON fields (and arrays in them, I've tried it and am using it). Before that I was just storing JSON strings into a TEXT column. It supports FTS3, FTS4, & JSON1
As of July 2019, it still gets version bumps every now and then, so it isn't a dead project.
https://www.sqlite.org/json1.html (store and query JSON documents)
https://www.sqlite.org/fts3.html (perform full-text searches)
https://github.com/app-z/Json-to-SQLite
At first generate Plain Old Java Objects from JSON http://www.jsonschema2pojo.org/
Main method
void createDb(String dbName, String tableName, List dataList, Field[] fields){
...
Fields name will create dynamically

Retrieving string from an array within a json object

I'll keep it short. I want to obtain the string value from "token". But from what I can see there is a "data" array within my json object. How do I retrieve the array and, specifically, the token string from this array?
{
"response":true,
"state":8,"data":{
"token":"$2a$08$oeH79FjMV6ZQTO.9qqfToujNBbst420Xx7o9jJdFgbJsJijtEpX\/O",
"numbers":[{
"number":"4581102282",
"enabled":"0",
"custid":"1528511113",
"route":"rock",
"clir":"2",
"mob":"1",
"clip":"",
"mbx":"0",
"trunk":"2",
"valid_from":"2011-09-07",
"valid_to":"2011-10-05"
}]
}
}
Simply use
JSONObject jsonObj=new JSONObject(response);
JSONObject jsonData=jsonObj.getJSONObject("data");
String token=jsonData.getString("token");

Trouble with Android JSONObject from Grails webservice

Okay I am quering data from a Grails webservice that returns JSON. The JSON when viewed with the JSONViewer app parses fine. When I take that same string and use JSONObject(string) in my Android app I get "value of String cannot be converted to JSONObject."
Here's my JSON string
[[{"class":"mygrails.TopTen","id":491,"ttAmount":14200000,"ttMlId":402,"ttRank":1,"ttWeekId":1108},{"class":"mygrails.MovieList","id":402,"mlApproved":1,"mlApprovedId":5,"mlMovieId":"GNOMEOAN","mlReleaseDate":"2011-03-08T07:41:45Z","mlTitle":"Gnomeo and Juliet","mlWeekId":1106}]]
Now the JSON is comes from the standard JSON conversion of a SQL data using render from the groovy file through the import grails.converters.JSON.
... //(call to render JSON in the groovy file)
def a
a = Table.findAll("from someTable as st where st.id=" params.id)
render a as JSON
...
So I am not sure what I doing wrong and why the JSON looks a little off to me. (still new to JSON)
In json if you see "[]" means its a json array and if you see "{}" it is an json object. Both of then can have the other nested inside then.
In your case the string the starts with json array.
So try something like the following
String str = "[[{"class":"mygrails.TopTen","id":491,"ttAmount":14200000,"ttMlId":402,"ttRank":1,"ttWeekId":1108},{"class":"mygrails.MovieList","id":402,"mlApproved":1,"mlApprovedId":5,"mlMovieId":"GNOMEOAN","mlReleaseDate":"2011-03-08T07:41:45Z","mlTitle":"Gnomeo and Juliet","mlWeekId":1106}]]";
JSONArray jsonArray = new JSONArray(str);
jsonArray = jsonArray.getJSONArray(0);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String class = jsonObject.getString("class"); // class will value "mygrails.TopTen"
Try to create an JSONArray from the String instead of JSONObject. I didn't test this but that should do the trick: you have two nested arrays that contain then actual data.
Check out your JSON online with http://jsonformat.com/
http://www.freeformatter.com/json-formatter.html
JSON Viewer
http://jsonviewer.stack.hu/
Paste your text in there and you can see what you should parse:

Categories

Resources