change JSON response in javascript - javascript

I am working on WEB API's, I have received the JSON response from server, NOW I want to change the returned response into different JSON's to show different table and graphs.
this is the response from server
{"data":[{"id":3663101,"lstImeis":[{"number":"14370340908558","maxDate":"2017-08-24 22:08:58.0","minDate":"2017-08-24 22:08:58.0"},{"number":"22418344742097","maxDate":"2017-08-24 18:08:56.0","minDate":"2017-08-24 18:08:56.0"}],"number2":789},{"id":3665337,"lstImeis":[{"number":"48717031235502","maxDate":"2017-08-24 21:09:38.0","minDate":"2017-08-24 21:09:38.0"},{"number":"42540009239622","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"},{"number":"42540009239644","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"}],"number2":456}]}
and I need to convert this response to look like this :
{"data":[{"id":3663101,"number":"14370340908558","maxDate":"2017-08-24 22:08:58.0","minDate":"2017-08-24 22:08:58.0","number2":789},{"id":3663101,"number":"22418344742097","maxDate":"2017-08-24 18:08:56.0","minDate":"2017-08-24 18:08:56.0","number2":789},{"id":3665337,"number":"48717031235502","maxDate":"2017-08-24 21:09:38.0","minDate":"2017-08-24 21:09:38.0","number2":456},{"id":3665337,"number":"42540009239622","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0","number2":456},{"id":3665337,"number":"42540009239644","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0","number2":456}]}
this is what I have done so far,
var data = new Array();
var str = '{"data":[{"id":3663101,"lstImeis":[{"number":"14370340908558","maxDate":"2017-08-24 22:08:58.0","minDate":"2017-08-24 22:08:58.0"},{"number":"22418344742097","maxDate":"2017-08-24 18:08:56.0","minDate":"2017-08-24 18:08:56.0"}],"number2":789},{"id":3665337,"lstImeis":[{"number":"48717031235502","maxDate":"2017-08-24 21:09:38.0","minDate":"2017-08-24 21:09:38.0"},{"number":"42540009239622","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"},{"number":"42540009239644","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"}],"number2":456}]}';
var response = JSON.parse(str);
var tableData = response.data;
var dataLength = response.data.length;
for (i = 0; i < dataLength; i++) {
var obj = {};
obj["id"] = tableData[i].id;
for (a = 0; a < tableData[i].lstImeis.length; a++) {
obj["number"] = tableData[i].lstImeis[a].number,
obj["maxDate"] = tableData[i].lstImeis[a].maxDate,
obj["minDate"] = tableData[i].lstImeis[a].minDate
}
obj["number2"] = tableData[i].number2;
data.push(obj);
}
console.log(JSON.stringify(data));
Here is JSFIDDLE
Outer loops works fines but inner loop gives the last updated data, If you look at the given data and changed data, you will see the difference.
Any idea, why its overriding the values in inner for loop and appending only last values into obj?

Your logic is incorrect line
var obj = {};
obj["id"] = tableData[i].id;
should be inside the inner loop. also line
obj["number2"] = tableData[i].number2;
data.push(obj);
should be moved inside the inner loop.
Here is the improved and working code https://jsfiddle.net/dmtfxt35/4/
var response = JSON.parse(str);
var tableData = response.data;
var finalObj = {data:[]};
for (var i in tableData){
for(var a in tableData[i].lstImeis){
var tmpObj = {};
tmpObj['id'] = tableData[i].id;
tmpObj["number"] = tableData[i].lstImeis[a].number;
tmpObj["maxDate"] = tableData[i].lstImeis[a].maxDate;
tmpObj["minDate"] = tableData[i].lstImeis[a].minDate;
tmpObj["number2"] = tableData[i].number2;
finalObj.data.push(tmpObj);
}
}
console.log(JSON.stringify(finalObj));
I hope this helped.

You using the same obj variable while inserting the data to array.
Obj is dict and it got updated on loop.
I have updated the code as per you requirement
<script>
var data = new Array();
var str = '{"msg":"success","code":"200","status":null,"data":[{"id":3663101,"lstImeis":[{"number":"14370340908558","maxDate":"2017-08-24 22:08:58.0","minDate":"2017-08-24 22:08:58.0"},{"number":"22418344742097","maxDate":"2017-08-24 18:08:56.0","minDate":"2017-08-24 18:08:56.0"}],"number2":789},{"id":3665337,"lstImeis":[{"number":"48717031235502","maxDate":"2017-08-24 21:09:38.0","minDate":"2017-08-24 21:09:38.0"},{"number":"42540009239622","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"},{"number":"42540009239644","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"}],"number2":456}],"draw":0,"limit":0,"recordsFiltered":0,"recordsTotal":0}';
var response = JSON.parse(str);
var tableData = response.data;
var dataLength = response.data.length;
for(i = 0; i < dataLength; i++){
for(a = 0; a < tableData[i].lstImeis.length; a++){
var obj = {};
obj["id"] = tableData[i].id;
obj["number"] = tableData[i].lstImeis[a].number;
obj["maxDate"] = tableData[i].lstImeis[a].maxDate;
obj["minDate"] = tableData[i].lstImeis[a].minDate;
obj["number2"] = tableData[i].number2;
data.push(obj);
}
}
console.log(JSON.stringify(data));
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

You can use array#reduce and array#map.
var response = [{"id":3663101,"lstImeis":[{"number":"14370340908558","maxDate":"2017-08-24 22:08:58.0","minDate":"2017-08-24 22:08:58.0"},{"number":"22418344742097","maxDate":"2017-08-24 18:08:56.0","minDate":"2017-08-24 18:08:56.0"}],"number2":789},{"id":3665337,"lstImeis":[{"number":"48717031235502","maxDate":"2017-08-24 21:09:38.0","minDate":"2017-08-24 21:09:38.0"},{"number":"42540009239622","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"},{"number":"42540009239644","maxDate":"2017-08-24 16:35:08.0","minDate":"2017-08-24 16:35:08.0"}],"number2":456}];
var result = response.reduce(function(res, obj) {
var temp = obj.lstImeis.map(function(o) {
return Object.assign({}, o, {id: obj.id, number2: obj.number2});
});
return res.concat(temp);
},[])
var output = {data: result};
console.log(JSON.stringify(output));

Look at this Fiddle. You were pushing last updated obj data. You had to push obj each time you update it.
for(i = 0; i < dataLength; i++){
for(a = 0; a < tableData[i].lstImeis.length; a++){
var obj = {};
obj["id"] = tableData[i].id;
obj["number"] = tableData[i].lstImeis[a].number;
obj["maxDate"] = tableData[i].lstImeis[a].maxDate;
obj["minDate"] = tableData[i].lstImeis[a].minDate;
obj["number2"] = tableData[i].number2;
data.push(obj);
}
}

Related

Faster way to collect data from JSON than looping in spreadsheet

I am learning Javascript and this is my first time working with Google Sheets Apps Script. What I am doing is taking a large JSON file and importing it into my sheet. After that I am populating a few hundred properties based on the key:value found in the JSON.
This is how it kinda works right now:
Go to first column and first row of my sheet.
Get the name (property name).
Search the JSON for the key and then grab the value.
Update a neighbor cell with the value found in the JSON.
Right now it all works the only issue is it seems to be pretty slow. It takes about .5-1 second per lookup and when I have 200+ properties it just seems slow. This might just be a limitation or it might be my logic.
My sheet can be found here: https://docs.google.com/spreadsheets/d/1tt3eh1RjL_CbUIaPzj10DbocgyDC0iNRIba2B4YTGgg/edit#gid=0
My function that does everything:
function parse() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange(2,1);
var range1 = sheet.getRange("A2");
var cell = range.getCell(1, 1);
var event_line = cell.getValue();
var tmp = event_line.split(". ");
var col_number = tmp[0];
var event_name = tmp[1];
event_json = get_json_from_cell(col_number);
const obj = JSON.parse(event_json);
var traits = obj.context.traits;
var properties = obj.properties;
//Get the range for the section where properties are
var traits_range = sheet.getRange("contextTraits");
var allprop = sheet.getRange("testAll");
var alllen = allprop.getNumRows();
var length = traits_range.getNumRows();
for (var i = 1; i < length; i++) {
var cell = traits_range.getCell(i, 1);
var req = traits_range.getCell(i, 4).getValue();
var trait = cell.getValue();
var result = traits[trait];
var result_cell = traits_range.getCell(i, 3);
if (result == undefined) {
if (req == "y"){
result = "MISSING REQ";
result_cell.setBackground("red");
} else {
result = "MISSING";
result_cell.setBackground("green");
}
} else {
result_cell.setBackground("blue");
}
result_cell.setValue(result);
Logger.log(result);
}
for (var i = 1; i < alllen; i++) {
var cell = allprop.getCell(i,1);
var req = allprop.getCell(i, 4).getValue();
var prop = cell.getValue();
var result = properties[prop];
var result_cell = allprop.getCell(i, 3);
if (result == undefined) {
if (req == "y"){
result = "MISSING REQ";
result_cell.setBackground("red");
} else {
result = "MISSING";
result_cell.setBackground("green");
}
} else {
result_cell.setBackground("blue");
}
result_cell.setValue(result);
}
Logger.log(result);
}

App Script create array from email string

Using app scripts I'm trying to extract all the email addresses from email messages and put them in an array. From my console.log messages, I'm getting stuck because it looks like instead of an array I just get a string. i'm not too familiar with javascript. Any help would be great. I'm looking for an array of email address. The methods of message.get() return a string. I want to split out the email address and create a single, unified array.
var ui = SpreadsheetApp.getUi();
function onOpen(e){
ui.createMenu("Gmail Manager").addItem("Get Emails by Label", "getGmailEmails").addToUi();
}
function getGmailEmails(){
var label = GmailApp.getUserLabelByName("MyLabel");
var threads = label.getThreads();
var fullArray = [];
for(var i = threads.length - 1; i >=0; i--){
var messages = threads[i].getMessages();
for (var j = 0; j <messages.length; j++){
var message = messages[j];
if (message.isUnread()){
fullArray.push(extractDetails(message));
}
}
}
console.log("FullArray:"+fullArray);
for(var i=0; i<fullArray.length; i++){
console.log("printing array " + i + ": "+fullArray[i])
}
}
function extractDetails(message){
var dateTime = message.getDate();
var subjectText = message.getSubject();
var senderDetails = message.getFrom();
var ccEmails = message.getCc();
var replyEmail = message.getReplyTo();
var toEmail = message.getTo();
var emailArray = []
var senderArray = senderDetails.split(',');
var ccArray = ccEmails.split(',');
var replyArray = replyEmail.split(',');
var toArray = toEmail.split(',');
for (var i =0 ; i<toArray.length; i++){
console.log("toArray Loop"+ i + " : "+ toArray[i]);
emailArray.push([toArray[i]]);
}
for (var i =0 ; i<ccArray.length; i++){
console.log("ccArray Loop"+ i + " : "+ ccArray[i]);
emailArray.push([ccArray[i]]);
}
console.log("Email Array: "+ emailArray);
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
activeSheet.appendRow([dateTime, senderDetails, subjectText, ccEmails,replyEmail,toEmail,emailArray]);
return emailArray;
}
I think the problem is just the console output. If you change console.log("Email Array: "+ emailArray); to console.log("Email Array: ", emailArray);, then it shows an array of arrays. You could simplify your extract method as follows:
function extractDetails(message) {
/* ... */
var senderDetails = message.getFrom();
var ccEmails = message.getCc();
var replyEmails = message.getReplyTo();
var toEmails = message.getTo();
let emailArray = [senderDetails, ccEmails, replyEmails, toEmails].reduce(
(array, string) => {
//remove names (like "Name <name#company.com>") and filter empty values
let emails = string
.split(/\s*,\s*/)
.map(e => e.replace(/.*?([^#<\s]+#[^#\s>]+).*?/g, "$1"))
.filter(Boolean);
if(emails.length > 0)
return array.concat(emails)
return array
}, []);
/* ... */
}

How can I convert CSV data to JSON data?

I wrote the code that gets data from a device.
The form of the data is csv. Below is the data value.
1,1.635946,1.636609,1.640240,1.636091
2,1.642825,1.640267,1.639013,1.636568
3,1.636835,1.636022,1.637664,1.637144
4,1.641332,1.641166,1.637950,1.640760
5,1.636041,1.637437,1.640702,1.633678
But I want the data in json format. So I tried using an online converter and got the following values:
[
{
"1": 2,
"1.635946": 1.642825,
"1.636609": 1.640267,
"1.640240": 1.639013,
"1.636091": 1.636568
},
{
"1": 3,
"1.635946": 1.636835,
"1.636609": 1.636022,
"1.640240": 1.637664,
"1.636091": 1.637144
}
]
What parts of my code should I modify if I want to get these values?
Below is my code.
var Timer;
var i = 0 ;
setTimeout(function(){
Timer = setInterval(function(){
port.write(meascommand+'\n');
i++;
if(i==5){
clearInterval(Timer);
}
},5000);
},1000);
port.on('data',function(devicevalue){
arrayvalue = devicevalue.toString();
eachvalue = arrayvalue.split(';');
var results = [];
var index = i ;
var ch0value = eachvalue[0] ;
var ch1value = eachvalue[1] ;
var ch2value = eachvalue[2] ;
var ch3value = eachvalue[3] ;
results[0] = index ;
results[1] = ch0value ;
results[2] = ch1value ;
results[3] = ch2value ;
results[4] = ch3value ;
console.log(results);
fs.appendFile(file,results+'\r\n',function(err){
if(err)
console.log(err);
});
});
};
function processFiles(files) {
var file = files[0];
var reader = new FileReader();
reader.onload = function (e) {
var output = document.getElementById("fileOutput");
var texto = e.target.result;
csvJSON(texto);
};
reader.readAsText(file);
}
function csvJSON(csv) {
var lines = csv.split("\n");
var result = [];
var headers;
for (var i = 0; i < lines.length; i++) {
headers = lines[i].split("\n");
}
var cont = 0;
for (var i = 0; i < lines.length; i++) {
var obj = {};
var currentline = lines[i].split("\n");
for (var j = 0; j < headers.length; j++) {
obj[cont] = currentline[j];
}
cont++;
result.push(obj);
}
return JSON.stringify(result); //JSON
}
Try to use below code. Credits go to: https://gist.github.com/iwek/7154578
NOTE: split(","). If lines contains , snippet won't work. But thats not the case in your data, as far as I can see.
function csvJSON(csv){
var lines=csv.split("\n");
var result = [];
var headers=lines[0].split(",");
for(var i=1;i<lines.length;i++){
var obj = {};
var currentline=lines[i].split(",");
for(var j=0;j<headers.length;j++){
obj[headers[j]] = currentline[j];
}
result.push(obj);
}
//return result; //JavaScript object
return JSON.stringify(result); //JSON
}

How to push json keys and json values into two arrays

I have one json file with following structure:
{
"won":3,
"lost":0,
"void":0,
"active":1
}
I've been trying to figure out how to make two arrays in javascript that will contain following information:
var labels = ["won", "lost", "void", "active"];
var data = ["3","0", "0","1"];
But I can't my head around how to do it.
You can use:
var labels = Object.keys(input);
var data = Object.values(input);
However you should check the browsers compatibility:
Object.keys
Object.values
Try this code
var item_list = {
"won":3,
"lost":0,
"void":0,
"active":1
};
var labels = [];
var data = [];
for(var item in item_list){
// console.log(item);
labels.push(item);
data.push(item_list[item]);
}
console.log(labels);
console.log(data);
You can do this too
var mydata = {
"won":3,
"lost":0,
"void":0,
"active":1
};
var first = [],
second = [];
for (var property in mydata) {
if ( ! mydata.hasOwnProperty(property)) {
continue;
}
first.push(property);
second.push(mydata[property]);
}
console.log(first);
console.log(second);
Try This :
var obj = {
"won":3,
"lost":0,
"void":0,
"active":1
}
var keys = new Array();
var values = new Array();
for (var key in obj) {
keys.push(key)
values.push(obj[key]);
}
console.log(keys);
console.log(values);

Get data out from callback inside query

I am trying to get data out from query inside the query. I tried everything without success.
getvideos() {
var sectionHolder = [];
var ids = [];
var titles = [];
var videos = [];
var challengeCl = Parse.Object.extend("Challenges");
var query = new Parse.Query(challengeCl);
query.find().then((data) => {
for(var key = 0; key < data.length; key++) {
var object = data[key];
var videoid = object.id;
var title = object.get('title');
ids.push(videoid)
titles.push(title)
var video = Parse.Object.extend('Videos');
var queryv = new Parse.Query(video);
queryv.equalTo('challengeid', videoid);
queryv.find().then(
(video) => {
videos.push(video); //i want to get this data out from this scope
});
sectionHolder.push({title: title, data: videos}); // <---- i want to put videos data in data
}
console.log(sectionHolder);
this.setState({listData: sectionHolder});
});
}
I read parse documentation but there is nothing related to this.

Categories

Resources