Home > Blockchain >  APPSCRIPT URL PARSING ISSUE
APPSCRIPT URL PARSING ISSUE

Time:12-11

Ive been looking for a api to fetch if a vehicle has a open recall. I was able to find the url to pass the vin number to chrylser and receive the info I need using url fetch. I am having a issue parsing out the retreived data though.

the request is to this address:

https://www.mopar.com/moparsvc/recallInfo?vin=1C4RJKBG4M8122507&mrkt=ca&language=en_ca&campaign_status=All&campaign_type=A&callback=showVinInfo&_=1637770607999

the output data looks like this:

showVinInfo({"vin_recall":[{"vin_status":"success","campaign_status":"success","last_system_update_date":"2021-11-24","vehicle":{"vin":"1C4RJKBG4M8122507","niv":"","vehicle_desc":"JEEP GRAND CHEROKEE L LIMITED 4X4","model_year":"2021"},"recall_details":{"recall":[{"fca_campaign_number":"Y67","agency_campaign_number":"2021-576","campaign_desc":"2021 WL Radio Software Update","owner_letter_url":"/webselfservice/pdf/ca_en/Y67.pdf","type_of_campaign":"SAFETY","vin_live_date":"2021-09-23","vin_campaign_status":"REPAIRED","condition_and_risk":"Vehicles may fail to conform to Canada Motor Vehicle Safety Standard (CMVSS) 111 - Mirrors and Rear Visibility Systems. Suspect vehicles may not display the rear view image during a backing event. The vehicle operator will notice that the rearview image is not displayed if attempting to reference the image while backing. If this warning is not heeded, backing without verifying it is safe to do so could lead to an increased risk of injury to people outside the vehicle.","repair_description":"Your dealer will inspect and if necessary, update the radio software version","agency_report_date":"2021-09-21","repair_date":"2021-09-24"},{"fca_campaign_number":"Y79","agency_campaign_number":"2021-690","campaign_desc":"2021 WL & 2022 WS - ORC DTC/warning lamp","owner_letter_url":"/webselfservice/pdf/ca_en/Y79.pdf","type_of_campaign":"SAFETY","vin_live_date":"2021-11-19","vin_campaign_status":"Incomplete but repair parts are available","condition_and_risk":"The Occupant Restraint Control (ORC) module on your vehicle may have the incorrect software. The airbag warning indicator may not illuminate to notify the operator of possible compromised airbag system functionality. If specific ORC internal faults are active, then a diagnostic trouble code (DTC) will not be set and the airbag warning indicator may not illuminate. The internal faults will disable deployment of both the driver and passenger airbag squib (related to airbag venting post deployment) and knee airbags. Reduced occupant protection in the event of a crash may result in an increased risk of injury to motor vehicle occupants.","repair_description":"Your dealer will reprogram the ORC module with correct software. The estimated repair time is about a half hour.","agency_report_date":"2021-11-10","repair_date":"9999-01-01"}]}}]})

for some reason i am getting SyntaxError: Unexpected token s in JSON at position 0

*** HERE IS MY CODE WITH ATTEMPT TO FIX****

function pullOpenRecall() {

  var vin = ("2C4RC1GG8LR137995");
  
  var results = UrlFetchApp.fetch("https://www.mopar.com/moparsvc/recallInfo?vin=" vin "&mrkt=us&language=en_us&campaign_status=All&campaign_type=A&callback=showVinInfo&_=1638229674507").getContentText();
 
const content = outputSample();
const json = JSON.parse(content.match(/^. ?\(({.*})\)$/)[1]);

console.log(json);

function outputSample() {
  return `showVinInfo({"vin_recall":[{"vin_status":"success","campaign_status":"success","last_system_update_date":"2021-11-24","vehicle":{"vin":"1C4RJKBG4M8122507","niv":"","vehicle_desc":"JEEP GRAND CHEROKEE L LIMITED 4X4","model_year":"2021"},"recall_details":{"recall":[{"fca_campaign_number":"Y67","agency_campaign_number":"2021-576","campaign_desc":"2021 WL Radio Software Update","owner_letter_url":"/webselfservice/pdf/ca_en/Y67.pdf","type_of_campaign":"SAFETY","vin_live_date":"2021-09-23","vin_campaign_status":"REPAIRED","condition_and_risk":"Vehicles may fail to conform to Canada Motor Vehicle Safety Standard (CMVSS) 111 - Mirrors and Rear Visibility Systems. Suspect vehicles may not display the rear view image during a backing event. The vehicle operator will notice that the rearview image is not displayed if attempting to reference the image while backing. If this warning is not heeded, backing without verifying it is safe to do so could lead to an increased risk of injury to people outside the vehicle.","repair_description":"Your dealer will inspect and if necessary, update the radio software version","agency_report_date":"2021-09-21","repair_date":"2021-09-24"},{"fca_campaign_number":"Y79","agency_campaign_number":"2021-690","campaign_desc":"2021 WL & 2022 WS - ORC DTC/warning lamp","owner_letter_url":"/webselfservice/pdf/ca_en/Y79.pdf","type_of_campaign":"SAFETY","vin_live_date":"2021-11-19","vin_campaign_status":"Incomplete but repair parts are available","condition_and_risk":"The Occupant Restraint Control (ORC) module on your vehicle may have the incorrect software. The airbag warning indicator may not illuminate to notify the operator of possible compromised airbag system functionality. If specific ORC internal faults are active, then a diagnostic trouble code (DTC) will not be set and the airbag warning indicator may not illuminate. The internal faults will disable deployment of both the driver and passenger airbag squib (related to airbag venting post deployment) and knee airbags. Reduced occupant protection in the event of a crash may result in an increased risk of injury to motor vehicle occupants.","repair_description":"Your dealer will reprogram the ORC module with correct software. The estimated repair time is about a half hour.","agency_report_date":"2021-11-10","repair_date":"9999-01-01"}]}}]})`;
 }
}

I probably should have been a little more clear on what Im attempting to do. Im building a google spreadsheet for my service dept. When they enter vehicles that need to be serviced to the spreadsheet I want the script to grab the vin from the spreadsheet and run the api call then return some of the values inside the response to the spreadsheet. I know currently my script has a variable set up for the vin but I figured I would add the code for pulling the vin from the spreadsheet after I could figure out how to even correctly parse the response to return certain values inside.

Currently the only way I know to parse then return specific values from a api call response is by code like this var foo = JSON.parse then accessing the it like (foo["Results"][0]["valueIwantToReturn"])

I know Im probably in over my head a little bit so I appreciate the help you are giving.

CodePudding user response:

It is JSONP, not JSON. You could extract the 'JSON part' to parse it to json:

const content = 'showVinInfo({...})';
const json = JSON.parse(content.match(/^. ?\(({.*})\)$/)[1]);

const content = outputSample();
const json = JSON.parse(content.match(/^. ?\(({.*})\)$/)[1]);
console.log(json);

function outputSample() {
  return `showVinInfo({"vin_recall":[{"vin_status":"success","campaign_status":"success","last_system_update_date":"2021-11-24","vehicle":{"vin":"1C4RJKBG4M8122507","niv":"","vehicle_desc":"JEEP GRAND CHEROKEE L LIMITED 4X4","model_year":"2021"},"recall_details":{"recall":[{"fca_campaign_number":"Y67","agency_campaign_number":"2021-576","campaign_desc":"2021 WL Radio Software Update","owner_letter_url":"/webselfservice/pdf/ca_en/Y67.pdf","type_of_campaign":"SAFETY","vin_live_date":"2021-09-23","vin_campaign_status":"REPAIRED","condition_and_risk":"Vehicles may fail to conform to Canada Motor Vehicle Safety Standard (CMVSS) 111 - Mirrors and Rear Visibility Systems. Suspect vehicles may not display the rear view image during a backing event. The vehicle operator will notice that the rearview image is not displayed if attempting to reference the image while backing. If this warning is not heeded, backing without verifying it is safe to do so could lead to an increased risk of injury to people outside the vehicle.","repair_description":"Your dealer will inspect and if necessary, update the radio software version","agency_report_date":"2021-09-21","repair_date":"2021-09-24"},{"fca_campaign_number":"Y79","agency_campaign_number":"2021-690","campaign_desc":"2021 WL & 2022 WS - ORC DTC/warning lamp","owner_letter_url":"/webselfservice/pdf/ca_en/Y79.pdf","type_of_campaign":"SAFETY","vin_live_date":"2021-11-19","vin_campaign_status":"Incomplete but repair parts are available","condition_and_risk":"The Occupant Restraint Control (ORC) module on your vehicle may have the incorrect software. The airbag warning indicator may not illuminate to notify the operator of possible compromised airbag system functionality. If specific ORC internal faults are active, then a diagnostic trouble code (DTC) will not be set and the airbag warning indicator may not illuminate. The internal faults will disable deployment of both the driver and passenger airbag squib (related to airbag venting post deployment) and knee airbags. Reduced occupant protection in the event of a crash may result in an increased risk of injury to motor vehicle occupants.","repair_description":"Your dealer will reprogram the ORC module with correct software. The estimated repair time is about a half hour.","agency_report_date":"2021-11-10","repair_date":"9999-01-01"}]}}]})`;
}

  • Related