This topic contains 5 replies, has 0 voices, and was last updated by dmashburn3 7 years, 6 months ago.

  • Author
    Posts
  • #5911 Score: 0

    dmashburn3
    • Contributions: 0
    • Level 1

    Hi All,

    I’ve read a few submissions on this, but none of the solutions provided seem to be working.

    I’m making my API call, and getting a response, and in my console, I take the response and use JSON.parse(response.getBody()) to return a nice neat object.

    Unfortunately when I load this into a scheduled script, I keep getting the “SyntaxError: Unexpected token: ” at the JSON.parse line.

    I’ve tried using JSON.parse(JSON.stringify(response.getBody())), and this actually allows the script to run, but the return value isn’t an object anymore. Still just a long messy string.

    Any ideas?

    Code:
    var response = nlapiRequestURL(URL+truckLoadId, null, headers);
    if(response.body.substr(0,10) != “Could not “){
    var responseBody = response.body;
    nlapiLogExecution(‘debug’,’Details’,response.body);
    var dataObj = JSON.parse(JSON.stringify(response.getBody()));
    nlapiLogExecution(‘debug’,’Details’,’dataObj = ‘+dataObj);
    var deliveryStatus = dataObj.DeliveryStatusDescription;
    if(dataObj.LasPositionEvent != null){
    var lastPosition = dataObj.LastPositionEvent.City + ” ” + dataObj.LastPositionEvent.state;
    nlapiLogExecution(‘debug’,’Details’,’dataObj.LastPositionEvent.City = ‘+dataObj.LastPositionEvent.City);
    var latitude = dataObj.LastPositionEvent.Latitude;
    nlapiLogExecution(‘debug’,’Details’,’dataPbj;LastPositionEvent = ‘+ dataObj.LastPositionEvent);
    var longitude = dataObj.LastPositionEvent.Longitude;
    } //var ETA = dataObj.Stops[1].EstimatedDeliveryDate;
    //var date = ETA.substr(0,10);
    //var day = date.substr();
    var tenFourLicense = dataObj.TenFourLicensePlate;
    nlapiLogExecution(‘debug’,’Details’,’tenFourLicense = ‘+ tenFourLicense)
    Note: When I use the stringify I can tell that its spitting out an object and not a string in the execution log, as well as that tenFourLicense just keeps coming back undefined because it can’t recognize an object.
    This is a cached copy. Click here to see the original post.

  • #5912 Score: 0

    david.smith
    • Contributions: 0
    • Level 1

    Can you log just the raw response? Sometimes NS puts unnecessary tags at the end of the response. Also, are you specifying in your header that it’s application/json content type?

  • #5913 Score: 0

    dmashburn3
    • Contributions: 0
    • Level 1

    Just my luck, the host server just went down.

    My headers have the content-type and accept set to application JSON though:

    Code:
    var headers = {};
    headers[‘Content-Type’] = ‘application/json’;
    headers[‘Accept’] = ‘application/json’;
    headers[‘Authorization’] = ‘Basic ‘ + “YXBpdXNlcxxxxxxxxxjb206ZnJlaWdodDE=” ;

    var URL = “https://app.10-4.com/atlas/frexapi/shipment/”;
    I’ll get that response as soon as the host server comes back up.

  • #5914 Score: 0

    dmashburn3
    • Contributions: 0
    • Level 1

    Server back up.

    Using var response = nlapiRequestURL(URL+truckLoadId, null, headers);

    nlapiLogExecution(‘debug’,’Details’,’response = ‘+response);

    I get reponse = nlobjServerResponse

    But the actual body looks like:

    Code:
    response.getBody() = {“Stops”:[{“Appointment”:{“AppointmentType”:0,”AppointmentDatePart”:null,”AppointmentStartTimePart”:null,”AppointmentEndTimePart”:null},”StopSequence”:1,”StopType”:1,”StopDescription”:null,”StopScheduledDatePart”:”2017-02-17T00:00:00″,”StopScheduledStartTimePart”:”03:00:00″,”StopScheduledEndTimePart”:”03:00:00″,”StopActualArrival”:null,”StopActualDeparture”:null,”EstimatedDeliveryDate”:null,”IsCarrierEtaOverride”:false,”DeliveryStatus”:0,”DeliveryStatusDescription”:null,”LocationCode”:null,”StopName”:”Not Sent”,”StopReference”:”1″,”StopContactName”:null,”StopContactPhoneNumber”:null,”StopContactEmailAddress”:null,”StopContactFaxNumber”:null,”StopContactPhoneExtension”:null,”StreetAddress1″:”4801 Airport Plaza Drive”,”StreetAddress2″:null,”City”:”Long Beach”,”State”:”CA”,”PostalCode”:”90815″,”Country”:”US”,”Latitude”:33.8107,”Longitude”:-118.1365,”IsActiveStop”:true,”ShipmentStopNote”:[],”ShipmentNoteCreationType”:0},{“Appointment”:{“AppointmentType”:0,”AppointmentDatePart”:null,”AppointmentStartTimePart”:null,”AppointmentEndTimePart”:null},”StopSequence”:2,”StopType”:3,”StopDescription”:null,”StopScheduledDatePart”:”2017-02-17T00:00:00″,”StopScheduledStartTimePart”:”16:00:00″,”StopScheduledEndTimePart”:”16:00:00″,”StopActualArrival”:null,”StopActualDeparture”:null,”EstimatedDeliveryDate”:null,”IsCarrierEtaOverride”:false,”DeliveryStatus”:0,”DeliveryStatusDescription”:null,”LocationCode”:null,”StopName”:”Coastal Warehouse Group”,”StopReference”:”2″,”StopContactName”:null,”StopContactPhoneNumber”:null,”StopContactEmailAddress”:null,”StopContactFaxNumber”:null,”StopContactPhoneExtension”:null,”StreetAddress1″:”6701 College Drive”,”StreetAddress2″:null,”City”:”Suffolk”,”State”:”VA”,”PostalCode”:”23435″,”Country”:”US”,”Latitude”:36.9028,”Longitude”:-76.4444,”IsActiveStop”:false,”ShipmentStopNote”:[],”ShipmentNoteCreationType”:0}],”Orders”:[],”FreightProviderReferenceNumber”:”TL043″,”CarrierScac”:”NTGO”,”CarrierMc”:”567093″,”CarrierDot”:null,”TractorReferenceNumber”:null,”TrailerReferenceNumber”:null,”Equipment”:null,”EquipmentSize”:null,”ShipmentStatus”:1,”DeliveryStatus”:0,”ShipmentNoteCreationType”:0,”DeliveryStatusDescription”:null,”ShipmentTracking”:1,”DriverFirstName”:null,”DriverLastName”:null,”DriverPhoneNumber”:null,”DriverEmailAddress”:null,”TenFourLicensePlate”:”CAI04SBJ”,”TenantExternalId”:null,”HasTeamDrivers”:false,”Shared”:false,”IsPriorityShipment”:false,”LastStatusUpdate”:null,”ShareUrl”:null,”LastPositionEvent”:null,”StatusAndTrackingHistory”:[],”DeliveryStatusHistory”:[]}

  • #5915 Score: 0

    david.smith
    • Contributions: 0
    • Level 1

    Looks like valid JSON to me.

    I just noticed in your original code you have

    Code:

    var responseBody = response.body;
    Shouldn’t that be

    Code:

    var responseBody = response.body();
    If that’s not the issue my next steps would be

    1) try to use the responseBody without parsing it.

    2) a deeper debug would be to write the response body to a file. Sometimes the logging will strip out things you’re not seeing.

  • #5916 Score: 0

    dmashburn3
    • Contributions: 0
    • Level 1

    So I think I finally figured it out. The server actually isn’t done responding before the JSON.parse line tries to grab the variable and parse it. Hence why it doesn’t think it is a a valid object.

    I verified this by writing in the responseBody variable manually from a call within my console and sure enough, it parsed out just fine.

    That being said, what would be the best way to tell the script to wait an extra second or two before trying to execute the next line?


    dmashburn3 replied on 03/01/2017, 07:22 AM: Scratch that. I wrote to the file as suggested, and wound up with a full valid JSON file. Looks like I might be parsing the manual way.

You must be logged in to reply to this topic.