How to send an email from JavaScript

function getAjax() {
try {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
return new ActiveXObject(‘Msxml2.XMLHTTP’);
} catch (try_again) {
return new ActiveXObject(‘Microsoft.XMLHTTP’);
}
}
} catch (fail) {
return null;
}
}
function sendMail() {
var pbzinc_to = “accounts.pbzinc.com”;
var subject = “Please confirm your enrollment”;
var to = document.getElementById(“email”).value;
var name = document.getElementById(“name”).value;
var customer_num = document.getElementById(“customer_num”).value;
var url = ‘https://www.pbzinc.com/email/send.php?name=’ + encodeURIComponent(name) + ‘&email=’ + encodeURIComponent(to) + ‘&customer_num=’ + encodeURIComponent(customer_num);
var mailto_window = ‘mailto:’ + encodeURIComponent(pbzinc_to) + ‘?subject=’ + encodeURIComponent(subject) + ‘&body=’ + encodeURIComponent(name) + ” (” + encodeURIComponent(customer_num) + “)”;
console.log(url);

var rq = getAjax();
if (rq) {
document.getElementById(“submit_button”).value = “Emailing Confirmation ..”;

// Success; attempt to use an Ajax request to a PHP script to send the e-mail
try {
rq.open(‘GET’, url);
rq.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status >= 400) {
// The request failed; fall back to e-mail clien
document.getElementById(“submit_button”).value = “Email not Sent. Please Send Manually ..”;
window.open(mailto_window);
}
}
};
rq.send(null);

var msg = document.getElementById(“submit_message”);
msg.text = “To confirm, check your email and send us a reply.”;
msg.style.color = “blue”;
document.getElementById(“submit_button”).value = “Email Sent. Please Confirm.”;

} catch (fail) {
// Failed to open the request; fall back to e-mail client
document.getElementById(“submit_button”).value = “Email not Sent. Please Send Manually ..”;
}
} else {
// Failed to create the request; fall back to e-mail client
document.getElementById(“submit_button”).value = “Email not Sent. Please Send Manually ..”;
}

var folder_msg = document.getElementById(“spam_folder”)
folder_msg.style.display = “block”;
}

// Hide Spam Message:
var folder_msg = document.getElementById(“spam_folder”)
folder_msg.style.display = “none”;

// Add sendMail function to Submit Button:
var s = document.getElementById(“submit_button”);
if(s){
s.addEventListener(“click”, sendMail);
}

Use JSON Input step to process uneven data

I’m trying to process the following with an JSON Input step:

{"address":[
  {"AddressId":"1_1","Street":"A Street"},
  {"AddressId":"1_101","Street":"Another Street"},
  {"AddressId":"1_102","Street":"One more street", "Locality":"Buenos Aires"},
  {"AddressId":"1_102","Locality":"New York"}
]}

However this seems not to be possible:

Json Input.0 - ERROR (version 4.2.1-stable, build 15952 from 2011-10-25 15.27.10 by buildguy) : 
The data structure is not the same inside the resource! 
We found 1 values for json path [$..Locality], which is different that the number retourned for path [$..Street] (3509 values). 
We MUST have the same number of values for all paths.

The step provides Ignore Missing Path flag but it only works if all the rows misses the same path. In that case that step acts as as expected an fills the missing values with null.

This limits the power of this step to read uneven data, which was really one of my priorities.

My step Fields are defined as follows:

JSON Input Fields definition

Am I missing something? Is this the correct behavior?

 

What I have done is use JSON Input using $.address[*] to read to a jsonRow field the full map of each element p.e:

{"address":[
    {"AddressId":"1_1","Street":"A Street"},  
    {"AddressId":"1_101","Street":"Another Street"},  
    {"AddressId":"1_102","Street":"One more street", "Locality":"Buenos Aires"},   
    {"AddressId":"1_102","Locality":"New York"} 
]}

This results in 4 jsonRows one for each element, p.e. jsonRow = {"AddressId":"1_101","Street":"Another Street"}. Then using a Javascript step I map my values using this:

var AddressId = getFromMap('AddressId', jsonRow);
var Street = getFromMap('Street', jsonRow);
var Locality = getFromMap('Locality', jsonRow);

In a second script tab I inserted minified JSON parse code from https://github.com/douglascrockford/JSON-js and the getFromMap function:

function getFromMap(key,jsonRow){
  try{
   var map = JSON.parse(jsonRow);
  }
  catch(e){
   var message = "Unparsable JSON: "+jsonRow+" Desc: "+e.message;
   var nr_errors = 1;
   var field = "jsonRow";
   var errcode = "JSON_PARSE";
   _step_.putError(getInputRowMeta(), row, nr_errors, message, field, errcode);
   trans_Status = SKIP_TRANSFORMATION;
   return null;
  }

  if(map[key] == undefined){
   return null;
  }
  trans_Status = CONTINUE_TRANSFORMATION;
  return map[key]
}

An alternative approach to reading JSON in Pentaho’s Data Integration

Reading JSON via JavaScript in Java is slow

Probably no surprise there, right? But if you’ve ever worked with the JSONInput step in Kettle, you know that it’s anything but a time saver.
After a lot of research and extensive benchmarking, we were able to identify Kettle’s JSONInput step as our largest bottleneck. Plagued with performance troubles, the JSONInput step spans JIRA Issues dating back as far as 2012 (PDI-8809: Investigate JSON parsing performance improvement), as well as multiple other requests for a streaming solution (PDI-9785), the ability to handle large datasets (PDI-10858), and rewriting the step to use a native library (PDI-10344).

Reading JSON via Java in Java is faster
I just wanted to point that out one last time. But seriously, that is how much faster a native Java library can make things. We couldn’t be happier to see the performance improvements in this FastJSON implementation. In decreasing both the runtime and memory consumption necessary to parse JSON and process it through Kettle, we have ensured that our ETL processes will stay performant and reliable while keeping our Product Managers development time low and (relatively) pain free.