If you are using the XRM.WebApi in your Dynamics 365 implementation, you could use the following JavaScript function to transform the incoming field values from Xrm.WebApi calls to the required data types.

Function:

function GetVal(result, attributeName, attributetype) { 
    if (!result[attributeName]) {
        return null;
    }
    switch (attributetype) {
        case "String":
            return result[attributeName];       
        case "Date":
            var aDate = new Date(result[attributeName]);
            return aDate;
        case "Optionset":
            return result[attributeName]; 
        case "Float":
            return parseFloat(result[attributeName]);  
        case "Int":
            return parseInt(result[attributeName]);  
        case "Lookup":
            var lookupValue = new Array();
            lookupValue[0] = new Object();
            lookupValue[0].id = result[attributeName];
            lookupValue[0].name = result[attributeName + "@OData.Community.Display.V1.FormattedValue"];
            lookupValue[0].entityType = result[attributeName + "@Microsoft.Dynamics.CRM.lookuplogicalname"];
            return lookupValue;  
        default:
            return null;
    }    
}

Usage:

function GetContactDetails(formContext, contactGuid) {
    
    Xrm.WebApi.retrieveRecord("contact", contactGuid.replace('{', '').replace('}', ''), "?$select=new_stringfield,birthdate,gendercode,address1_latitude,_new_lookupfield_value").then(

function success(result) {   
           		formContext.getAttribute("new_stringfield2").setValue(GetVal(result, "new_stringfield", "String"));
            		formContext.getAttribute("new_dateofbirth").setValue(GetVal(result, "birthdate", "Date")); 
		formContext.getAttribute("new_gender").setValue(GetVal(result, "gendercode","Optionset"));            
           	 	formContext.getAttribute("new_foatfield").setValue(GetVal(result, "address1_latitude", "Float"));   
            		formContext.getAttribute("new_mylookup").setValue(GetVal(result, "_new_lookupfield_value", "Lookup"));
       	 },
        	Function (error) {
            		Xrm.Navigation.openAlertDialog(error.message);
        	}
    );
}

Note: The lookup field name is “new_lookupfield” but in order to get the value, use “_new_lookupfield_value”.