Solved, Isaac is the best!!!
here is the code to separate the first and last name based on the "Primary_Contact" key if available or the job name if not.
var firstname = ''
var lastname = ''
if (query4.data.hasOwnProperty('primary_contact')) {firstname = query4.data.primary_contact.name.substring(0, query4.data.primary_contact.name.indexOf(' '))
lastname = query4.data.primary_contact.name.substring( query4.data.primary_contact.name.indexOf(' ')+1)}
else {firstname = query4.data.name.substring(0, query4.data.name.indexOf(' '));
lastname = query4.data.name.substring( query4.data.name.indexOf(' ')+1)}
var fullName = query4.data
fullName.firstname = firstname;
fullName.lastname = lastname;
return fullName