I am trying to persist a json object to an sqlite2 database in javascript( using node) roughly as mentioned here by writing it in a 'text' field. However, when I query the field back, all the " quotes in the json string are preceded by escape character '\' which ruins the json format. My question is does sqlite3 insert these escape characters because I am definitely not writing them or is my json malformed? Is there a way to escape these? If this does not work, then I will try to store the json as a blob. Please find relevant code snippets and other approaches I have tried to make this work below: Create the json object and write using an sqlite3 insert statement:
var collection = {collection: {
title: req.body.collectionTitle,
owner: req.body.ownerName,
id:req.body.collectionID,
items: {list:[]}
}};
console.log(JSON.stringify(collection));
//prints {"collection":{"title":"abc","owner":"Alice","id":"1","sharedWith":{"users":[]},"items":{"list":[]}}}
var statement = "INSERT INTO collection ( tree) values ('"+
JSON.stringify(collection) + "')";
//execute statement
statement = "SELECT tree from collection where blah blah";
//execute statement
db.all(err, function(err, rows){
console.log(JSON.stringify(rows[0]));
//prints
//{"tree":"{\"collection\":{\"title\":\"abc\",\"owner\":\"Alice\",\"id\":\"1\",\"sharedWith\":{\"users\":[]},\"items\":{\"list\":[]}}}"}
// because of the '\' , the object is no longers recognized as a valid json. is this the standard behavior?
});
I explored the JSON1 extension for sqlite3 as well but could not use it because: 1. compiled the amalgamation with json enabled option but i am using npm sqlite3 wrapper which does not have JSON support enabled apparently as I got the same behaviour when I changed the field type to JSON. 2. Tried to load json1 as a loaded extension by building it by db.load_extension was giving a 'module not recognized error' so gave up. I am trying to use this npm module on top of storing this data as a text to help with managing, that is why my json has the format shown.
I know this is an inefficient way (I should have used a noSQL database maybe since I am trying to store a tree strucutre as json but moving to noSQL is not an option)
I even tried to parse this malformed json 'string' to remove the \ but its not a valid string. maybe i could try and convert it to a string first...
Any insight is greatly appreciated! Thanks!
via fah
No comments:
Post a Comment