How to pretty print json objects to db using Node-orm2 - json

I'm using node-orm2 to persist a model to a mysql db column. It works great, except I want the written json pretty printed, so I can easily read it when I view the DB.
Is this possible?

you can use JSON stringify with params:
var obj = {manu: true, gili: "abc"}
var str = JSON.stringify(obj, null, 4); // spacing = 4

Related

Convert escaped string cookie into json object using javascript.typescript

I am working on an angular 4 app and using ngx-cookie-service module to save and get the cookies
save cookie with following code
let defaultData = {
merchant_name : response.data.profile.business_name,
role : response.data.role,
id : response.data.merchant_id,
is_default : false
};
let cookieData = JSON.stringify(defaultData);
cookieService.set('merchant_data', cookieData );
it works properly and saves cookie in following format
%257B%2522merchant_name%2522%253A%2522vk%2522%252C%2522role%2522%253A%2522merchant%2522%252C%2522id%2522%253A103%252C
Now I am reading the same cookie with cookieService.get('merchant_data') and its works properly but I facing issue while converting the string to original json object.
I tried JSON.parse but its not working as its escaped string, is there any way to read these type of cookies strings and convert to json or array ?
JSON.parse() is exact opposite of JSON.stringify() and it should work.
The problem might come from ngx-cookie-service i would recommend using localStorage to accomplish same behaviour.
Like this :
var testObject = { 'one': 1, 'two': 2, 'three': 3 };
// Put the object into storage
localStorage.setItem('testObject', JSON.stringify(testObject));
// Retrieve the object from storage
var retrievedObject = localStorage.getItem('testObject');
console.log('retrievedObject: ', JSON.parse(retrievedObject));
More about HTML5 LocalStorage

How to insert Json values into Azure TableStorage?

Here is my code
I am trying to insert Json values into Azure as Entity, but I always get only the first value when I get back from Azure..Is there anything I need to change in the below code?
string Emulators = "['TestEmulator1','Testemulator12','Testemulator2','TestEmulator3']";
List<string> Emulators = new List<string>();
EmulatorIDS.Add(Emulators);
var jsondata = JsonConvert.SerializeObject(Emulators);
Console.WriteLine(jsondata);
var EmulatorID = new EntityProperty(jsondata);
//Instantiate serializer
Random rnd = new Random();
int a = rnd.Next(1, 1000);
DynamicTableEntity actordata = new DynamicTableEntity();
actordata.RowKey = "Test"+a.ToString();
actordata.PartitionKey = "test#gmail.com";
actordata.Properties["DetailsValue"] = EmulatorID;
actordata.Properties["OtherData"] = EmulatorID;
TableOperation insertOperation = TableOperation.Insert(actordata);
table.Execute(insertOperation);
Have a look https://www.nuget.org/packages/ObjectFlattenerRecomposer/ version 2.0 it will automatically convert your complex objects to a writable form to table storage, it supports complex types and ICollection/IEnumerable type properties as well. You do not need to do any up front json conversion yourself either, the api automatically handles conversions.
If anybody is looking for answer for this..Please see here about how to insert Json values via TableEntity instead of using JsonPayload format

Insert json string to mongoDB

I am new to Scala and MongoDB. I am writing a Scala program to read JSON string (it can be nested json string also) and insert the same to Mongo DB.
I tried below few things but its not working.
1) Tried to create an Document for the JSON string as below:
var document = (DBObject) JSON.parse(jsonString)
but getting the error
"value JSON is not member of object com.mongodb.DBObject".
2) Also tried with bson.Document as below but still can get it working
var myDoc = (Document.parse(schemajson))
Can anyone help me out on this? Please let me know if my approach is correct. If not the please do let me know what all things I need to do.
adding code:
val hadoopRDD = sc.textFile(data_file).foreach(line =>
{
data_array.clear
line.split("\t").foreach(word => data_array += word)
println(data_array)
var final_json = (schemajson.format(data_array: _*))
var document = (DBObject) JSON.parse(jsonString)
in above code final_json is the string having Json string like {"name": xyz, "age": 22}
I found the answer and below is the solution which worked for me.
I was simply using
var document = (DBObject) JSON.parse(jsonString)
which was causing error.
Instead we need to provide asInstanceOf[DBObject] which worked in my case
val dbObject: DBObject = JSON.parse(final_json_str).asInstanceOf[DBObject]

How to format a json string and show result in table?

I am using CakePhp _serialize method to make a web service and show the data in JSON format.I used the .json extension at the end of the URL to show this data.Now i want to show this data in table.Output image is attached.Is this possible then how i can do it?
Thanks
The format is a bit odd. I would prefer something like: "task_list": [ .... ]; iterating over objects is always a bit tedious.
Here is the jQuery code:
var data = ...;
var items = data["task_list"];
var table = $('<table/>');
table.appendTo($('body'));
$.each(items, function(id, value) {
var tr = $('<tr/>');
table.append(tr);
$('<td/>').text(id).appendTo(tr);
$('<td/>').text(value).appendTo(tr);
});

Couchbase - deserialize json into dynamic type

I'm trying to deserialize some JSON coming back from couchbase into a dynamic type.
The document is something like this so creating a POCO for this would be overkill:
{
UsersOnline: 1
}
I figured that something like this would do the trick, but it seems to deserialize into a dynamic object with the value just being the original JSON
var jsonObj = _client.GetJson<dynamic>(storageKey);
results in:
jsonObj { "online": 0 }
Is there anyway I can get the couchbase deserializer to generate the dynamic type for me?
Cheers
The default deserializer for the client uses .NET's binary serializer, so when you save or read a JSON string, it's just a string. GetJson will always just return a string. However, there are a couple of options:
You could convert JSON records to Dictionary instances:
var appJson = "{ \"UsersOnline\" : 1, \"NewestMember\" : \"zblock\" }";
var result = client.ExecuteStore(StoreMode.Set, "userCount", appJson);
var item = client.GetJson<Dictionary<string, object>>("userCount");
Console.WriteLine("There are {0} users online. The newest member is {1}.",
item["UsersOnline"], item["NewestMember"]);
Or you could use a dynamic ExpandoObject instance:
var appJson = "{ \"UsersOnline\" : 1, \"NewestMember\" : \"zblock\" }";
var result = client.ExecuteStore(StoreMode.Set, "userCount", appJson);
dynamic item = client.GetJson<ExpandoObject>("userCount");
Console.WriteLine("There are {0} users online. The newest member is {1}.",
item.UsersOnline, item.NewestMember);
In either case you're losing static type checking, which seems like it's OK for your purposes. In both cases you get access to the JSON properties without having to parse the JSON into a POCO though...
Edit: I wrote a couple of extension methods that may be useful and blogged about them at http://blog.couchbase.com/moving-no-schema-stack-c-and-dynamic-types