Deleting spesific array data in a JSON data in Mysql in Nodejs - mysql

I have an column like that :
{"MyTablets": [], "MyDepertmans": {"BeforeSales": [], "OnSales": [], "Followers": [], "AfterSales": [], "CustomerController": [], "CardCreators": [], "CardControllers": [], "FinanceProject": [], "FinanceOffical": [], "FinanceController": [], "FinanceCustomer": [], "FinancePurchase": [], "Purchase": [], "WareHouseRequest": [], "WareHouseOut": [], "WareHouseController": [], "WarehouseAcceptance": [], "WarehouseReturn": []}, "MyCustomers": [], "MySuppliers": [], "MyGoverments": [], "MyAdmins": [], "WaitingResponse": [100,110,120,150], "SendResponse": []}
I want to delete an specific data in WaitingResponse .. for example 110 value...
How can I do that in mysql and Json ...
I tried that :
const query = `SELECT JSON_SET(content, '$.WaitingResponse', JSON_REMOVE(JSON_EXTRACT(content, '$.WaitingResponse'), 100)) AS SONUC from mytablename WHERE id = ? LIMIT 1`;
but that delete all values and make data "WaitingResponse": null

Related

Add JSON data fields names Angular

I have data with no field names, I would like to append to each row a field name
[
"James Vega",
"23",
"abc#test.com"
],
I want to add the fields to this JSON data, I'm working on Angular 6
[
{ "name": "James Vega" },
{ "Age": 23 },
{ "email": "abc#test.com" }
],
You just use map function to transform the object you want
let arr=
[
[
"James Vega",
"23",
"abc#test.com"
]
];
let arr_transform=arr.map(el=> ({
name:el[0],
age:el[1],
email:el[2]
}));
console.log(arr_transform)
If your order of property would be the same, then it is possible to create a mapping and map it with O(1):
const foo = [
"James Vega",
"23",
"abc#test.com"
];
const mapping = {0: 'name', 1: 'Age', 2: 'email'};
const result = foo.map((el, i) => ({[mapping[i]]: el}));
console.log(result);

How to make a Google script call to a Django Rest framework

I have a an API available as a Django rest framework. I would like to make it available as a google sheet using Google script as less tech guys are more used to Exel. This a sample body with the input data I'm using with postman to do a post request:
{
"Group": {
"Names": [
"Peter",
"John",
"Marry"
],
"weightsColumns": [
"Peter",
"John",
"Marry"
],
"weightsIndex": [
0,
1,
2
],
"weightsValues": [
[
0.848324434,
0.00129090761,
0.00000549100261
],
[
0.68104794,
0.00000594297639,
0.00000477060816
],
[
0.496659891,
0.00000495070405,
0.00000227036433
],
[
0.326632792,
0.0000148350248,
0.00000133351815
]
]
},
"DataColumns": [
"Scale1",
"Scale2",
"Scale3"
],
"DataValues": [
[
0,
10,
100
]
],
"DataIndex": [
0,
1,
2
]
}
Now how can I do such an API call with Google script. My attempt so far following the official documentation
var data = {
"Group": {
"Names": [
"Peter",
"John",
"Marry"
],
"weightsColumns": [
"Peter",
"John",
"Marry"
],
"weightsIndex": [
0,
1,
2
],
"weightsValues": [
[
0.848324434,
0.00129090761,
0.00000549100261
],
[
0.68104794,
0.00000594297639,
0.00000477060816
],
[
0.496659891,
0.00000495070405,
0.00000227036433
],
[
0.326632792,
0.0000148350248,
0.00000133351815
]
]
},
"DataColumns": [
"Scale1",
"Scale2",
"Scale3"
],
"DataValues": [
[
0,
10,
100
]
],
"DataIndex": [
0,
1,
2
]
};
var options = {
'method' : 'post',
'contentType': 'application/json',
'Authorization': 'ApiKey name:XXXXXX',
'payload' : JSON.stringify(data)
};
UrlFetchApp.fetch('sampleurl', options);
However with that I get an error with a bad request. But the data is working with postman so that I believe I handle the request wrongly with google script. Any help would be appreciated
try doing as so:
var options = {
"headers": {
'Authorization': 'Bearer ' + token,
"Content-type": "application/json",
},
"method": "POST",
"payload": JSON.stringify(data)
}
use the headers key for Authorization and Content-type. Not sure if you need Bearer with your api call

How to Remove and update in one obj of child obj in JSON Using mysql

Given the following JSON stored inside a MySQL json data type:
var data = ' [
{
"key": 1,
"step": 6,
"param": [
{"key_1": "test1"},
{"key_2": "test2"},
{"key_3": "test3"}
]
},
{
"key": 4,
"step": 8,
"param": [
{"key_4": "test4"},
{"key_5": "test5"}
]
}
]';
I need to remove key_3 in param obj also update removed data in mysql using one query.
**Note:**I Don't know the key_3 equal value, I Have only key_1 want to remove {"key_1":"test1"}
OUTPUT
[
{
"key": 1,
"step": 6,
"param": [
{"key_2": "test2"},
{"key_3": "test3"}
]
},
{
"key": 4,
"step": 8,
"param": [
{"key_4": "test4"},
{"key_5": "test5"}
]
}
]
Have you tried the function JSON_REMOVE in your attempts to achieve what you want?

How to get specified values from ansbile facts

I'm confused a little bit.
I alredy tried jmespath, but didn't help.
I need to get a high-level object which contains a specified object.
In the example below we see an ansible fact about HDDs. I need to get the disk name (sdf, sdg or dm-0) of the HDD which contains a specified partition, e.g. sdf1.
I've got this jmespath query:
msg.*.[partitions.sdf1]
but it just shows me all inside sdf1. Filters like [?partitions=="sdf1"] don't work here,
so the question is: how to preserve the whole sdf object in my example?
thanks in advance!
{
"msg": {
"sdf": {
"partitions": {
"sdf1": {
"holders": [],
"links": {
"ids": [
"17101686F123-part1",
"wwn-0x123456-part1"
]
},
"sectors": "1875380224"
}
},
"removable": "0",
"rotational": "0"
},
"sdg": {
"partitions": {
"sdg1": {
"holders": [],
"links": {
"ids": [
"164414123CEB-part1",
"wwn-0x1233451234831ceb-part1"
]
},
"uuid": "F301-FA7F"
}
},
"removable": "0"
},
"dm-0": {
"holders": [],
"host": "",
"links": {
"ids": [],
"uuids": []
},
"vendor": null,
"virtual": 1
}
}
}
This should work:
msg.*.{value: #, condition: partitions.sdf1}[?condition].value
explanation
At first we create a temporary object For each HDD object: {value, condition}. condition will be null if partitions.sdf1 doesn't exist on the corresponding HDD (sdf, sdg dm-0) object. If partitions.sdf1 exists, condition will contain that object, that is:
"condition": {
"holders": [],
"links": {
"ids": [
"17101686F123-part1",
"wwn-0x123456-part1"
]
},
"sectors": "1875380224"
}
Using [?condition] you filter out all objects where condition is null. Finally, we extract the actual value using .value

Unable to get the length of JSON object

I am getting 'TypeError: Cannot convert undefined or null to object' while trying to access the length of json object in nodejs.
Following is how my data looks like:
{
"college": [
{
"colleges": [],
"department": [
1,
2,
3
],
"general_course": [],
"id": 1,
"name": "College of the Arts",
"short_name": "",
"url": "/content.php?catoid=16&navoid=1919"
},
{
"colleges": [],
"department": [
4,
5,
6
],
"general_course": [],
"id": 2,
"name": "College of Communications",
"short_name": "",
"url": "/content.php?catoid=16&navoid=1920"
},
{
"colleges": [],
"department": [
7,
12
],
"general_course": [],
"id": 3,
"name": "College of Education",
"short_name": "",
"url": "/content.php?catoid=16&navoid=1921"
},
{
"colleges": [],
"department": [
13,
17,
19
],
"general_course": [],
"id": 4,
"name": "College of Engineering and Computer Science",
"short_name": "",
"url": "/content.php?catoid=16&navoid=1922"
},
{
"colleges": [],
"department": [
20,
26,
27
],
"general_course": [],
"id": 5,
"name": "College of Health and Human Development",
"short_name": "",
"url": "/content.php?catoid=16&navoid=1923"
},
{
"colleges": [],
"department": [
28,
29,
32,
48
],
"general_course": [],
"id": 6,
"name": "College of Humanities and Social Sciences",
"short_name": "",
"url": "/content.php?catoid=16&navoid=1924"
},
{
"colleges": [],
"department": [
52,
57
],
"general_course": [],
"id": 7,
"name": "College of Natural Sciences and Mathematics",
"short_name": "",
"url": "/content.php?catoid=16&navoid=1925"
},
{
"colleges": [],
"department": [
58,
59,
63
],
"general_course": [],
"id": 8,
"name": "Mihaylo College of Business and Economics",
"short_name": "",
"url": "/content.php?catoid=16&navoid=1926"
}
]
}
Step 1 - Parsing it into nodejs:
let colleges = JSON.parse(data)
Step 2 - Saving it into the dialogflow app data:
app.data.collegeData = data;
Step 3 - Accessing the length:
let collegeLength = Object.keys(app.data.collegeData.college).length;
Getting following error in firebase console:
TypeError: Cannot convert undefined or null to object
Update:
Here is the code:
if ( app.data.collegeData === undefined ){
app.data.collegeData = [];
}
**Step 1 =>**
showColleges(college);
**Step 2 =>**
function showColleges(collegeName){
if (app.data.collegeData.length === 0){
getCollegeData().then(buildSingleCollegeResponse(collegeName))
.catch(function (err){
console.log('No college data')
console.log(err)
});
}
else{
buildSingleCollegeResponse(collegeName);
}
}
**Step 3 =>**
function getCollegeData(){
console.log('Inside get College Data')
return requestAPI(URL)
.then(function (data) {
let colleges = JSON.parse(data)
if (colleges.hasOwnProperty('college')){
saveData(colleges)
}
return null;
})
.catch(function (err) {
console.log('No college data')
console.log(err)
});
}
**Step 4 =>**
function saveData(data){
app.data.collegeData = data;
console.log(app.data.collegeData)
}
**Step 5 =>**
function buildSingleCollegeResponse(collegeName){
let responseToUser, text;
//console.log('Data is -> '+ Object.keys(app.data.collegeData.college).length);
//console.log('Length is -> '+ app.data.collegeData.college.length);
console.log('Count is -> '+app.data.collegeCount);
let collegeLength = Object.keys(app.data.collegeData.college).length;
if ( collegeLength === 0){
responseToUser = 'No colleges available at this time';
text = 'No colleges available at this time';
}
else if ( app.data.collegeCount < collegeLength ){
for ( var i = 1; i <= collegeLength; i++)
{
console.log('All Colleges:: '+app.data.collegeData.college[i])
let coll = app.data.collegeData.college[i]
let name = coll.name
console.log('checkCollegeExist => College Name:: '+ name)
console.log('checkCollegeExist => Parameter => College Name:: '+collegeName)
if(String(name).valueOf() === String(collegeName).valueOf()){
responseToUser = 'Yes! CSUF has '+collegeName;
text = 'Yes! CSUF has '+collegeName;
}else{
responseToUser = 'CSUF does not teach ' +collegeName+' currently';
text = 'CSUF does not teach ' +collegeName+' currently';
}
}
}
else{
responseToUser = 'No more colleges';
}
if (requestSource === googleAssistantRequest) {
sendGoogleResponse(responseToUser);
} else {
sendResponse(text);
}
}
This is the culprit:
getCollegeData().then(buildSingleCollegeResponse(collegeName))
That calls buildSingleCollegeResponse(collegeName) and then passes its return value into then, just like foo(bar()) calls bar and passes its return value into foo.
You wanted to pass a functon to then:
getCollegeData().then(() => buildSingleCollegeResponse(collegeName))
// An arrow function ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Note that now that it's clear from the updated question that app.data.collegeData.college is an array, there's no need for Object.keys. Change:
let collegeLength = Object.keys(app.data.collegeData.college).length;
to simply
let collegeLength = app.data.collegeData.college.length;
Arrays have a length property (whereas non-array objects don't, by default).
You haven't mentioned what value app.data holds before issuing app.data.collegeData = data;
If app.data is undefined you should try app.data = {collegeData: data}
Or else negating what app stores. Following works
app = {};
app.data = {};
app.data.collegeData = data;
app.data.collegeData.college.length
You don't need to do the following
let collegeLength = Object.keys(app.data.collegeData.college).length;
Update: Refer to https://jsfiddle.net/dmxuum79/3/