I have a json format. Now i want to replace all the p: from my json and covert into real name like p:OrderId to OrderId, p:Firstname to Firstname.
Please check below JSON.
{"status":"success","msg":{"$":{"xmlns:p":"342","xmlns:xsi":"test
link","xsi:schemaLocation":"324","source":"234234"},"p:Customer":[{"p:OrderId":["asdasd"],"p:FirstName":["Ingrid"],"p:LastName":["Bryne"],"p:EmailAddress":["asd#ad.com"],"p:BirthDate":["1965-09-23"],"p:CellularPhone":["123465798"],"p:City":["asd"],"p:Country":["asdda"],"p:PostalCode":["23332"],"p:StreetAddress":["asd"],"p:PartnerId":["1-84OPG"],"p:CommunicationPreferences":[{"p:Phone":["12"],"p:Email":["123"],"p:Mail":["231"],"p:SMS":["Nei"]}],"p:ServiceAgreement":[{"p:AgreementType":["Bredbånd
- Privat"],"p:AgreementStartDate":["123"],"p:AgreementStatus":["ASS"],"p:Comment":["\n\nse.
\n\n"],"p:ListOfAssets":[{"p:Asset":[{"p:ProductPartNumber":["234"],"p:Comments":[""]},{"p:ProductPartNumber":["123"],"p:Comments":[""]},{"p:ProductPartNumber":["12313"],"p:Comments":[""]},{"p:ProductPartNumber":["123"],"p:Comments":[""]},{"p:ProductPartNumber":["123"],"p:Comments":[""]},{"p:ProductPartNumber":["123123"],"p:Comments":[""]}]}]}]}]}}
Output : https://prnt.sc/hwc157
Note : I have convert my real values to demo values. I need to change Key names with values.
Please try the below code:
var jsn = '{"status":"success","msg":{"$":{"xmlns:p":"342","xmlns:xsi":"test link","xsi:schemaLocation":"324","source":"234234"},"p:Customer":[{"p:OrderId":["asdasd"],"p:FirstName":["Ingrid"],"p:LastName":["Bryne"],"p:EmailAddress":["asd#ad.com"],"p:BirthDate":["1965-09-23"],"p:CellularPhone":["123465798"],"p:City":["asd"],"p:Country":["asdda"],"p:PostalCode":["23332"],"p:StreetAddress":["asd"],"p:PartnerId":["1-84OPG"],"p:CommunicationPreferences":[{"p:Phone":["12"],"p:Email":["123"],"p:Mail":["231"],"p:SMS":["Nei"]}],"p:ServiceAgreement":[{"p:AgreementType":["Bredbånd - Privat"],"p:AgreementStartDate":["123"],"p:AgreementStatus":["ASS"],"p:Comment":["se."],"p:ListOfAssets":[{"p:Asset":[{"p:ProductPartNumber":["234"],"p:Comments":[""]},{"p:ProductPartNumber":["123"],"p:Comments":[""]},{"p:ProductPartNumber":["12313"],"p:Comments":[""]},{"p:ProductPartNumber":["123"],"p:Comments":[""]},{"p:ProductPartNumber":["123"],"p:Comments":[""]},{"p:ProductPartNumber":["123123"],"p:Comments":[""]}]}]}]}]}}';
var ret = jsn.replace(/p:/g,'');
var obj = JSON.parse(ret);
console.log(obj);
Related
I have a solution for filtering on this question.
This works perfectly with a column that has string values. When I try to filter a column with numeric values it will not work. I'm assuming it is because .setHiddenValues() will not accept numeric values. I could be wrong.
Let me explain my scenario:
The user inputs a value on an HTML interface, let's say 6634.
The HTML calls my function on .gs and passes the numeric value the user inputted.
google.script.run //Executes an Apps Script JS Function
.withSuccessHandler(updateStatus) //function to be called upon successfull completion of Apps Script function
.withFailureHandler(failStatus)
.withUserObject(button) //To pass the event element object
.projectSearch2(projectID); //Apps Script JS Function
return;
The function (on the linked question above) will take that value and bump it up against the values in a column deleting the value if it is found. What I am left with is an array of values that I do not want filtered.
function projectSearch2(projectID){
var ss = SpreadsheetApp.getActive();
var monthlyDetailSht = ss.getSheetByName('Data Sheet');
var monLastCN = monthlyDetailSht.getLastColumn();
var monLastRN = monthlyDetailSht.getLastRow();
var data = monthlyDetailSht.getRange(1,1,1,monLastCN).getValues();//Get 2D array of all values in row one
var data = data[0];//Get the first and only inner array
var projectIDCN = data.indexOf('Project Id') + 1;
//Pull data from columns before filtering
var projectIDData = monthlyDetailSht.getRange(2,projectIDCN,monLastRN,1).getValues();
//Reset filters if filters exist
if(monthlyDetailSht.getFilter() != null){monthlyDetailSht.getFilter().remove();}
//Start Filtering
var projectIDExclCriteria = getHiddenValueArray(projectTypeData,projectID); //get values except for
var rang = monthlyDetailSht.getDataRange();
var projectIDFilter = SpreadsheetApp.newFilterCriteria().setHiddenValues(projectIDExclCriteria).build();//Create criteria with values you do not want included.
var filter = rang.getFilter() || rang.createFilter();// getFilter already available or create a new one
if(projectID != '' && projectID != null){
filter.setColumnFilterCriteria(projectIDCN, projectIDFilter);
}
};
function getHiddenValueArray(colValueArr,visibleValueArr){
var flatUniqArr = colValueArr.map(function(e){return e[0];})
.filter(function(e,i,a){return (a.indexOf(e.toString())==i && visibleValueArr.indexOf(e.toString()) ==-1); })
return flatUniqArr;
}
That array is used in .setHiddenValues() to filter on the column.
Nothing is filtered however. This works for all columns that contain string values, just not columns with numeric values. At this point I'm lost.
Attempted Solutions:
Convert user variable to string using input = input.toString(). Did not work.
manually inserted .setHiddenValues for projectIDExclCriteria. Like this: var projectIDFilter = SpreadsheetApp.newFilterCriteria().setHiddenValues([1041,1070,1071,1072]).build(); That succeeded so I know the issue is before that.
Step before was calling getHiddenValueArray. I manually inserted like so: var projectIDExclCriteria = getHiddenValueArray(projectIDData,[6634]); It is not working. Is the issue with that getHiddenValueArray function not handling the numbers properly?
Here is a solution. Changing the following:
.filter(function(e,i,a){return (a.indexOf(e.toString())==i && visibleValueArr.indexOf(e.toString()) ==-1); })
To:
.filter(function(e,i,a){return (a.indexOf(e) == i && visibleValueArr.indexOf(e) == -1); })
That works! Thank you Tanaike. The next question is will this impact columns that are not numeric. I have tested that and it works as well.
How about this modification?
From :
.filter(function(e,i,a){return (a.indexOf(e.toString())==i && visibleValueArr.indexOf(e.toString()) ==-1); })
To :
.filter(function(e,i,a){return (a.indexOf(e) == i && visibleValueArr.indexOf(e) == -1); })
Note :
In this modification, the number and string can compared using each value.
If you want to use return (a.indexOf(e.toString())==i && visibleValueArr.indexOf(e.toString()) ==-1), you can achieve it by modifying from colValueArr.map(function(e){return e[0];}) to colValueArr.map(function(e){return e[0].toString();}).
In this modification, colValueArr.map(function(e){return e[0].toString();}) converts the number to string, so the number is used as a string.
Reference :
Array.prototype.indexOf()
I have an array and want to fetch some values from array which has Strings as key.Please suggest how can i retrieve those values from array have string as key.
Code for Controller is:
var ultColumn=undefined;
$scope.ultColm="Attained Age";
for(var i=0;i<5;i++){
ultColumn=ultrowCellData[i][$scope.ultColmn];//This is not working
}//ultrowCellData contains the array
Please suggest how to get the value of key "Attained Age"
You can use angular.forEach(); For example:
angular.forEach(yourArray, function(value, key){
if(typeof key === 'string'){
console.log("Your result is here :", value);
}
});
Thanks.
If screenshot which you provided shows data included in ultrowCellData then it is not an array but map - var something = ultrowCellData['Attained Age '] will assign value 28 to something
You seem to have forgotten a space at the end of your key, on line 2.
Try Attained Age<space> instead of Attained Age (replace <space> with an actual space).
Notice, though, that this is a non-standard use for an Array, as arrays usually use only numbers as keys.
if at all possible, try using an Object instead.
I am trying to select a specific entry from a JSON but can not solve how to do it. The JSON that I get from the server (can not be modified) is:
[{"cid":"PWER","data":[{"1458496671000":464}],"sid":"728834","units":"kWm","age":0}]
What I need to get is the 464 (Power in Watts). The problem is, that the key is a timestamp and changes all the time. So far I tried
json[0].data[0]
but this leaves me with
{ '1458496779000': 464 }
Any ideas how I select the next value?
Thanks a lot!
You can do this -
var obj = json[0].data[0];
var key = Object.keys(obj)[0];
var data = obj[key];
// data is your value which should be 464.
I have data inside a data attribute, like so:
<div class="dashboard-module" data-rows="new Date(2013,10,04),12,"OR"##new Date(2013,10,17),2,"OR"##new Date(2013,10,09),2,"CA""></div>
Im trying to split this string up and use it in the data.addRow function:
rows = el.data('rows');
rowsarray = rows.split('##');
// Error: Row given with size different than 3 (the number of columns in the table).
$.each(rowsarray, function(index, value) {
data.addRow( [value] );
});
// the following works
data.addRow([new Date(2013,10,04),12,"OR"]);
data.addRow([new Date(2013,10,09),2,"CA"]);
data.addRow([new Date(2013,12,12),14,"AL"]);
I guess the commas inside the new date are being counted as different parts of the array?
I'm assuming that the double-quotes inside your data-rows attribute are escaped (otherwise the HTML is malforned).
When you call rowsarray = rows.split('##');, you are getting an array of strings, like this:
[
'new Date(2013,10,04),12,"OR"',
'new Date(2013,10,17),2,"OR"',
'new Date(2013,10,09),2,"CA"'
]
not an array of arrays. If you want to store your data in an HTML attribute, your best bet is to use a JSON-compatible format. The problem then becomes storing dates, since Date objects are not JSON-compatible, but that is easy to work around. Store your data like this instead:
[["Date(2013,10,04)",12,"OR"],["Date(2013,10,17)",2,"OR"],["Date(2013,10,09)",2,"CA"]]
I did two things with the data-rows attribute: first, I changed the dates from a format like new Date(2013,10,17) to a string like "Date(2013,10,17)". Second, I converted the string to a JSON string representation of an array of arrays (which uses the standard javascript array brackets [ and ]). Note that JSON requires the use of double-quotes for all internal strings, so you must either escape all internal strings to use with the data-rows attribute, or use single-quotes around the data-rows attribute string (eg: data-rows='<string>').
You can then parse that string for entry into your DataTable:
rows = JSON.parse(el.data('rows'));
// convert date strings to Date objects
for (var i = 0; i < rows.length; i++) {
var dateStr = rows[i][0];
var dateArr = dateStr.substring(5, dateStr.length - 1).split(',');
rows[i][0] = new Date(dateArr[0], dateArr[1], dateArr[2]);
}
data.addRows(rows);
I have such objects in ScriptDb,
[{a:1,b:2,c:3},{a:0,b:0}]
How do I query object without key c?
It seems the only way is to query all object using db.query({}), then use something like "typeof result.c == 'undefined'".
Is there a way to do it in ScriptDb?
Thanks.
You can use that to get records without c:
var db = ScriptDb.getMyDb();
var result = db.query({c: db.not(db.anyValue())});
while (result.hasNext()) {
var current = result.next();
Logger.log ("a= "+current.a+", c="+current.c);
}
The ones with c:
var result = db.query({c: db.anyValue()});
These functions (not, anyValue...) are documented in Class ScriptDbInstance