I am new to couchbase and exploring specific use case scenario as part of .Net API development. I want to use NoSql as a cache database however I am getting data in Json format and I want to store this raw Json data into couchbase. I do not have specific objects defined with specific properties to retrieve and store in it. I want to get this data in dynamic object where I can assign data dynamically. I found .Net native object called ExpandoObject which is derived from dynamic class which can store and assign data dynamically in run time. Please advice if this is possible with couchbase.
Hi @sunilsamawar,
When you say you are getting “data in Json format”, what does that mean? Are you getting a JSON string and trying to store that in Couchbase via .NET?
Yes I am getting in JSON string from Oracle. I wan to send and use that data in my react App.
I want to bring some clarity here. I am getting data from Oracle in dataset and serializing into JSON to store in NoSql. I consume this cache data for React App instead of hitting Oracle again which very slow.
Have you tried something like this:
var json = "{ 'foo': 'bar'}";
bucket.Insert(new Document<dynamic>
{
Id = "mydocid1",
Content = JsonConvert.DeserializeObject(json)
});
I don’t know if this is the most efficient way (perhaps @jmorris could weigh in), but using Json.net to deserialize the object works.
If you’re trying to do a raw, black box caching layer for the data coming out of Oracle, I’d recommend sending it to Couchbase as a byte array instead of a JSON object. Just take the raw string you get from Oracle, serialize it as UTF-8 to a byte using Encoding.Utf8.GetBytes(…), then store that. The Couchbase SDK will recognize the byte array and store it within Couchbase as such, skipping all the JSON magic. To get it back out, just use GetDocument<byte>(…).
yes I am aware this but I was curious about getting data. My question is how to get this data in dynamic object if json string is too long (have around 1.5 MB data). Do we need to iterate every and each field in document and assign to dynamic object such as .Net ExpandoObject that I have to pass to Front End.
Thanks Brant. This can be possible however rather I would prefer inbuilt couchbase cache provider I think that stores in binary. That will allow us to just store and retrieve raw data in cache. In this case we do not have any control on data which we store and retrieve. In my case, I may need to manipulate and filter data before storing into couchbase and later want to get only required data in dynamic object and pass to UI.
Use the dynamic type as T as it will take on the form of the JSON during serialization/deserlization as @matthew.groves said :
var getResult = bucket.Get<dynamic>("mydocid1");
You don’t need an ExpandoObject explicitly.
-Jeff
Thanks Jeff. I will try and let you know when we complete implementation.
You can also use JObject from Newtonsoft (https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm) which allows to access all properties and nodes of a JSON object.