Extract field from nested JSON object - tabulator

I have a JSON result set that contains nested JSON objects in each row. Is there a way to define a column in the table as "object.field"?

Yes, you can do exactly that. Say that your row data looks like this:
{
id:1,
user:{
name:"steve",
age:23
},
col:"red",
cheese:true
}
If you wanted to setup a column so that it looks at the name property of the user object you can set the field to user.name
var table = new Tabulator("#example-table", {
columns:[
{title:"Name", field:"user.name"}, //link column to name property of user object
],
});
Checkout the Nested Data Documentation for more info.

Related

how to insert multiple records from json array in sequelize?

learning sequelize ORM, say if the request body has array of values, shown below. when we have multiple users in an array in the request body, how to save it to the database , do we iterate over the array items or is there some kind of function to create multiple records at once?
//post('/save', async(request, response) => {
let new_users = request.body.users;
})
request body
{
"category": "students",
"department" : "engineering",
"users" : [ {name: foo, age:12}, {name:bar , age:18}]
}
user table columns
id
category
department
name
age
You can use the static bulkCreate method of a model to create multiple records at once:
await userModel.bulkCreate(users)
See bulkCreate

Insert a whole json Without creating different node in mongodb

i have a collection named drop down and i want to insert all the drop down static JSON values in a single collections without creating different node
for eg this is my json
"education": [
"Master",
"Bachelor"
],
"diet": [
"Veg",
"Non Veg"
]
Later on if i need to add more drop down values i don't have to alter the mongoose schema ,i could directly insert the json list
Is it possible or not? Sorry i am new to the Mongodb
Finally an idea strike on my mind and got the appropriate answer of my own question and like to share here. What i did is , I create the Schema as
const mongoose = require('mongoose');
const dropDownSchema = new mongoose.Schema({dropDown : mongoose.Schema.Types.Mixed})
module.exports.DropDownList =mongoose.model("DropDownList",dropDownSchema);
later on while passing data from the client
{
"dropDown":
{
"education": [
"Master"
,"Bachelor"
],
"diet": [
"Veg"
,"Non Veg"
]
}
}
While inserting data i first delete all data and insert the fresh one from the API
First find the document in that collection. Assume that we are requesting with the document _id and we are using mongoose model named Dropdown.
Example:
const document = await Dropdown.findOne({_id});
//Once you have found the document
// Add or change the existing fields
document[new_value_name] = new_values.map(val => val);
await document.save();
If you want to store it in array of object in mongodb then you can also create a schema with Array of dropdowns
var DropdownSchema = new mongoose.Schema({
dropdowns:[{
dropdown_key: { type: String },
dropdown_values: [{type: String}]
}]});
While inserting data you will need to loop through Object.keys of your json list and insert into as array . You can add number of dropdown as it is stored as array of object.
Please let me know if it helps.

DynamoDB update inside an array of objects (nodejs)

I noticed that DynamoDB can add and remove items from an array but how do you search for an specific item inside an object if you want to update that one specifically?
For example:
In MongoDB you can search for someitem.$.subitem and update that specific item.
Is there a way on how to do this with DynamoDB?
Item: {
someitem: [
{
subitem: "id",
somevalue: "something"
}
]
}
I would say this is basic functionality but seems not easy to find (or even unsupported)
AWS does not permit to modify it in a single update request more info was found in the following answers:
updating-a-json-array-in-aws-dynamodb.
The solution that they propose is to change the schema from array to {}, or to implement a custom functions and iterate through each array and find your new id to update, so to speak to programatically update your json and then insert whole object.
TableName : 'tablename',
Key : { id: id},
ReturnValues : 'ALL_NEW',
UpdateExpression : 'set someitem['+`index`+'].somevalue = :reply_content',
ExpressionAttributeValues : { ':reply_content' : updateddata }
array element edit via array index

Replace key-value pair in an object with Google Apps Script

I am making a call to an API for a commercial product using Apps Script. Unfortunately, the resulting object has several key-value pairs that contain the id from a linked table.
I can easily get the standard values and have written code to find the related name value and add it to the object. I would prefer to add the name in the same location as the original id. But, when I add a new key, it is added to the end of the object.
I want the name in the same location as id so when I insert it into a sheet, the columns will still be in order.
This is my object:
var json = {id: 4730183,name: "A A", customer_source_id:123, company: "NE Company"};
This is my desired object after replacing the id with the name:
var json = {id: 4730183,name: "A A", source:"CRM", company: "NE Company"};
Basically, I want to find customer_source_id in the object and replace it with source.
I can't use indexOf and splice because the object is not an array.
What is the best way to do this? Do I have to convert it to an array first and then back again?
A quick answer would be:
var obj = {id: 4730183,name: "A A", customer_source_id:123, company: "NE Company"};
var json = JSON.stringify(obj);
json = json.replace("customer_source_id","source")
The better answer is:
#Waqar Ahmed is correct. JavaScript objects are unordered. In your example "var json" is an object not JSON. You can make it JSON with JSON.stringify(json). But once the JSON is parsed into an object it again becomes unordered. You should not use it to store ordered data.
I am not sure if it is efficient, but you can iterate through the keys and build a new json object like this:
var newjson = {};
for(var key in json){
if(key === 'customer_source_id'){
newjson.source = [NEW VALUE TO DISPLAY];
}else{
newjson[key] = json[key];
}
}
json = newjson;
But, like #Waqar and #Spencer said, the object is not used for ordered data.
You can do his only in java script array. Not in JSON. JSON is meant to be addressed by keys, not by index.Change your json to
var json ={id: 4730183,name: "A A", customer_source_id:null, items : [] company: "ESI"};
Now you can insert items using push method of array.
json.items.push('My Item');

save object Id from one table to another table

I am getting ObjectId from one table and want to save another table which has column field name 'owner' with datatype 'Pointer<_User>'. But when I tried to do this I got this error 'invalid type for key owner, expected *_User, but got string'.
Please help me out. Thanks.
The problem lies in the title of the question. The operand to a pointer column isn't an object id, it is an object.
So if you have an object, say a PFUser, do this:
// user is a PFUser
myObjectThatPointsToUser("owner", user);
If you have only a user's object id as a string, ask yourself why. To the extent you can, design to converse in objects, not ids. But if you find yourself in this situation, set the pointer column like this:
// userId is the id of a PFUser
var userStub = { "__type": "Pointer",
"className": "_User",
"objectId": userId };
myObjectThatPointsToUser("owner", userStub);

Resources