Format an object to split the header of the data - arrays

I have a Typescript project where I want to change the format of a JSON object. The object I receive is an array of objects with the keys and the values, what I want is to separate the keys in the first array and then the data in each array.
The object I currently have is the following:
[
{
"id": "1",
"ser": null,
"IP": null,
"host": "",
"type": "Web",
"auth": ""
},
{
"id": "2",
"ser": null,
"IP": "191.174.230.02",
"host": "",
"type": "Proxy",
"auth": ""
}
]
This is what I want to achieve:
[
"id",
"ser",
"IP",
"host",
"type",
"auth"
],
[
"1",
null,
null,
"",
"Web",
""
],
[
"2",
null,
"191.174.230.02",
"",
"Proxy",
""
]
This is the object that I want to get by adding the values property to it:`
{
"values": [
[
"id",
"ser",
"IP",
"host",
"type",
"auth"
],
[
"1",
null,
null,
"",
"Web",
""
],
[
"2",
null,
"191.174.230.02",
"",
"Proxy",
""
]
]
}

use Object.keys(data[0]) to get the keys of the first element
iterate all elements with Object.values(element) to get each element data
push key and element value to array
Here is the code.
var data = [
{
id: "1",
ser: null,
IP: null,
host: "",
type: "Web",
auth: "",
},
{
id: "2",
ser: null,
IP: "191.174.230.02",
host: "",
type: "Proxy",
auth: "",
},
];
var arr = [Object.keys(data[0])];
for (let element of data) {
arr.push(Object.values(element));
}
var obj = { values: arr };

Related

How to populate array inside array of object in mongoose

i have a classShcema,inside my marksSchema i have class property i.e populated from classSchema.note that classSchema has subject and it is populate from subjectSchema.
const classSchemaDef = new mongoose.Schema({
classes:{type:Number},
subject:[{
type:mongoose.Types.ObjectId,
ref:'subject',
require:true,
default:null
}],
section:[String],
created_by:commonSchema.created_by,
},commonSchema.Trigger)
below is the Marksschema that has class in it. the class is populated but subject is not being populate.
const MarksSchemaDef = new mongoose.Schema(
{
student: {
type: mongoose.Types.ObjectId,
required: true,
ref: "student",
default: null,
},
marks_obtain: [
{
class: {
type: mongoose.Types.ObjectId,
ref: "class",
required: true,
default: null,
},
subject: {
type: mongoose.Types.ObjectId,
ref: "subject",
},
marks: {
type: Number,
required: true,
},
created_by: commonSchema.created_by,
},
],
},
commonSchema.Trigger
);
below is the code to retrive data from marksModel and populate the data.
getAllMarks = async (skip, limit) => {
return await MarksModel.find()
.skip(skip)
.limit(limit)
.populate("student")
.populate("marks_obtain.class")
.populate("marks_obtain.class.subject")
.populate("marks_obtain.subject");
}
below is the response data i got
{
"_id": "63ce6eb551a5815f8662fd87",
"student": {
"parentname": {
"fullname": "mohan shrestha",
"phone": 868594
},
"_id": "63c80a2912d13e1af652df71",
"studentname": "mogina shrestha",
"class": 8,
"image": "localhost:3000/pic.jpg",
"address": "dhanusmode",
"phone": 2675442,
"section": "a",
"created_by": null,
"__v": 0
},
"marks_obtain": [
{
"class": {
"_id": "63a6a17590f43440d694cedb",
"classes": 8,
"subject": [
"63a42fbd14407d172f07586d",
"63a42fd814407d172f075873"
],
"section": [
"a",
"b",
"c"
],
"created_by": null,
"__v": 0
},
"subject": {
"_id": "63a42fd814407d172f075873",
"subjectname": "grammer",
"__v": 0
},
"marks": 55,
"created_by": null,
"_id": "63ce6eb551a5815f8662fd88"
}
],
"__v": 0
}
],

I can't use .insert to add object into array with Dart

I'm trying to insert an object into array of objects. The idea is just add the content of the object if the object already exist but I don't understand why I can't use the .insert
I have tried using the .insert and adding the object into the array.
void main() {
var arr = [
{
"shelf1": [
{
"Entry": "something",
"Status": "",
"Account": "",
"Address": "",
"Result": null
}
]
},
{
"shelf2": [
{
"Entry": "something",
"Status": "",
"Account": "",
"Address": "",
"Result": null
}
]
},
{
"shelf3": [
{
"Entry": "something",
"Status": "",
"Account": "",
"Address": "",
"Result": null
}
]
}
];
var object = {
"shelf1": {
"Entry": "something2",
"Status": "",
"Account": "",
"Address": "",
"Result": null
}
};
arr.insert(0, object);
print(arr);
}
The arr has a type of List<Map<String, Map<String, List<Map<String, String>>>>> and the object you declared has a type of Map<String, Map<String, String>>. This is because using var, the type is automatically inferred based on the assigned value. Since you're trying to add an object of an incorrect type an error is thrown.
So your new object should be made like the following to be inserted normally:
var object = {
"shelf1": [{ // note that I made this inside a List []
"Entry": "something2",
"Status": "",
"Account": "",
"Shelf": "shelf",
"Address": "",
"Result": null
}]
};

Adding elements within Object TypeScript

Hello everyone i have dynamically expanding object which consists of a 2 x dimensional array. I
try to add in field label:{ } new key:value {"hg":"Hg"} object.
object JSON:
[
{
"callbackQueryData": "tNLQy3VcX",
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "Cll8xZbVo6",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
},
"label": {
"en": "New button"
}
},
{
"callbackQueryData": "qntufUVhz",
"label": {
"en": "New button"
},
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
}
}
],
[
{
"callbackQueryData": "cx46ECYG9",
"label": {
"en": "New button"
},
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
}
}
],
[
{
"callbackQueryData": "uHp5yd3Li",
"label": {
"en": "New button"
},
"method": {
"value": "",
"property": "",
"linkUrl": "",
"inside": null,
"type": "NEXT_PAGE",
"nextId": "",
"validation": null,
"calendar": null,
"condition": null,
"api": null,
"pagination": null
}
}
],
[]
]
I try cast it to simple array and via forEach() addressing everyone elements button:any and to add object which i need. But Spread syntax(...) can't find argument forEach().Or I'm doing it completely wrong.
let arrayButton:IBotButton[][] = ([] as IBotButton[][]).concat(...page.callbacks)//create simple array
arrayButton.forEach((button:any)=>{
...button,
label: { ...button.label, [languageSelect]: buttonText }
})
Assume your arrayButton looks like this:
const arrayButton: IBotButton[][] = [
[
{
label: { "en": "new Button-1" }
// ...
},
{
label: { "en": "new Button-2" }
// ...
},
//...
],
[
//...
]
];
And have these variables:
const languageSelect: string = "jp";
const buttonText: string = "new Button-3";
Then you could modify the label by following code:
// arrayButton is 2d array
arrayButton.forEach((array) => {
// array is 1d array
array.forEach((button, buttonIdx) => {
array[buttonIdx] = {
...button,
label: {
...button.label,
[languageSelect]: buttonText,
},
};
});
});

Search in Embedded Documents in MongoDB?

I have document as shown
[
{
"Users": [
{
"Name": "Kartikey Vaish",
"_id": "1",
},
{
"Name": "Witcher Proxima",
"_id": "2",
}
],
"_id": "12",
},
{
"Users": [
{
"Name": "Witcher Proxima",
"_id": "2",
},
{
"Name": "Saga",
"_id": "4",
}
],
"_id": "13",
}
]
I want to search for those documents whose Users array has that particular ID
For Example if
ID == 1 // should return
[
{
"Users": [
{
"Name": "Kartikey Vaish",
"_id": "1",
},
{
"Name": "Witcher Proxima",
"_id": "2",
}
],
"_id": "12",
}
]
ID == 2 // should return
[
{
"Users": [
{
"Name": "Kartikey Vaish",
"_id": "1",
},
{
"Name": "Witcher Proxima",
"_id": "2",
}
],
"_id": "12",
},
{
"Users": [
{
"Name": "Witcher Proxima",
"_id": "2",
},
{
"Name": "Saga",
"_id": "4",
}
],
"_id": "13",
}
]
ID == 4 // should return
[
{
"Users": [
{
"Name": "Kartikey Vaish",
"_id": "1",
},
{
"Name": "Saga",
"_id": "4",
}
],
"_id": "13",
}
]
As you can see from above my query should return only those objects whose "Users" array contains an object with given ID. I tried this but it doesn't work.
const chats = await Chats.find({
Users: { $elemMatch: { _id: "1" } },
});
// this returns an empty array
const chats = await Chats.find({
Users: { $elemMatch: { Name: "Kartikey Vaish" } },
});
// this returns
[
{
"Users": [
{
"Name": "Kartikey Vaish",
"_id": "1",
},
{
"Name": "Witcher Proxima",
"_id": "2",
}
],
"_id": "12",
}
]
What am I doing wrong here?
Is it something related to _id paramter?
EDIT:
My Chats Schema looks like this -
const Chats = mongoose.model(
"Chats",
new mongoose.Schema({
Users: {
type: Array,
required: true,
default: [],
},
})
);
Update your schema as shown below
const Chats = mongoose.model(
"Chats",
new mongoose.Schema({
Users: [{
_id: {
type: String,
required: true, // Include only if needed!
unique: true // Include only if needed!
},
Name: {
type: String,
index: true // Include only if needed!
}
}]
})
);
If you do not explicitly mention _id MongoDB will create _id field as ObjectId.

Mule Dataweave null check and array empty check of JSON payloads

Need to check null and empty array in Dataweave using filter or any other logic.
First check the parentId is null, we need to skip the particular JSON. If parentId is not null need to check the mobileContacts or emailContacts
have the array of values. If mobileContacts and emailContacts have empty List we need skip the particular JSON values. If any one have the value we need to process the records.
Input JSON:
{
"name": "XYZ",
"age": 23,
"results": [
{
"parentId": "12345",
"notes": "proceed",
"mobileContacts": [
{
"relationId": "12345",
"callId": "3456213"
},
{
"relationId": "12345",
"callId": "12345"
}
],
"emailContacts": [ ],
"initial": true
},
{
"parentId": "435638",
"notes": "proceed",
"mobileContacts": [ ],
"emailContacts": [ ],
"initial": true
},
{
"parentId": null,
"notes": "proceed",
"mobileContacts": [
{
"relationId": "12345",
"callId": "3456213"
},
{
"relationId": "12345",
"callId": "12345"
}
],
"emailContacts": [ ],
"initial": true
}
]
}
Need below Output:
{
"name": "XYZ",
"age": 23,
"results": [
{
"parentId": "12345",
"notes": "proceed",
"mobileContacts": [
{
"relationId": "12345",
"callId": "3456213"
},
{
"relationId": null,
"callId": null
}
],
"initial": true
}
]
}
How about this,
%dw 1.0
%output application/json
---
{
name: payload.name,
age: payload.age,
results: payload.results filter ($.parentId != null and ((sizeOf $.mobileContacts) > 0 or (sizeOf $.emailContacts) > 0))
}

Resources