I created a script to do an API call to a website I use for 3PL logistics and I'm trying to format the data.
Here's the script below.
function rateCarrier() {
var source_sheet = SpreadsheetApp.getActiveSheet();
var shipmentdate = source_sheet.getRange("A3").getValue();
var originzip = source_sheet.getRange("B3").getValue();
var origincity = source_sheet.getRange("C3").getValue();
var originstate = source_sheet.getRange("D3").getValue();
var destinationzip = source_sheet.getRange("B6").getValue();
var destinationcity = source_sheet.getRange("C6").getValue();
var destinationstate = source_sheet.getRange("D6").getValue();
var skidcount = source_sheet.getRange("B9").getValue();
var weight = source_sheet.getRange("C9").getValue();
var freightclass = source_sheet.getRange("D9").getValue();
var json =
'{'
' "RateQuote": {'
' "Origin": {'
' "City": "' origincity '",'
' "StateOrProvince": "' originstate '",'
' "ZipOrPostalCode": "' originzip '",'
' "CountryCode": "USA"'
' },'
' "Destination": {'
' "City": "' destinationcity '",'
' "StateOrProvince": "' destinationstate '",'
' "ZipOrPostalCode": "' destinationzip '",'
' "CountryCode": "USA"'
' },'
' "Items":['
' {'
' "Weight": "' weight '",'
' "Class": "' freightclass '"'
' }'
' ], '
' "PickupDate": "' shipmentdate '"'
' },'
'}';
//var payload = JSON.stringify(json);
var headers = {
"apiKey": "deleted",
"Content-Type": "application/json",
"Authorization": "Basic _authcode_"
};
var options = {
"method": "POST",
"contentType": "application/json",
"headers": headers,
"payload": json
};
var url = "https://api.rlc.com/RateQuote";
var response = UrlFetchApp.fetch(url, options);
if (200 == response.getResponseCode())
{
Logger.log('HTTP request succeeded');
}
else
{
Logger.log('HTTP request failed');
}
var data = JSON.parse(response.getContentText());
Logger.log(data);
}
It prints the results as this:
{Messages=[], Code=200.0, RateQuote={Charges=[{Amount=$3,956.00, Weight=5000, Rate=$79.12, Title=Class: 60 / Rated 50, Type=}, {Amount=$3,956.00, Rate=, Title=Gross Charge, Type=GROSS, Weight=5000}, {Amount=$3,698.86, Type=DISCNT, Rate=93.5%, Title=R L Discount Saves This Much, Weight=}, {Rate=, Amount=$257.14, Type=DISCNF, Title=Discounted Freight Charge, Weight=}, {Rate=47.3%, Type=FUEL, Title=Fuel Surcharge, Amount=$121.63, Weight=}, {Title=Net Charge, Type=NET, Weight=, Amount=$378.77, Rate=}], Ocean=null, PickupDate=11/10/2022, Destination={CountryCode=USA, City=DYERSVILLE, StateOrProvince=IA, ZipOrPostalCode=52040}, DestinationServiceCenter={Phone=1-800-318-2452, Code=DVP, Location=Davenport, IA, City=Davenport, ZipCode=52806-1010, State=IA, Address1=2940 W. 73rd Street, Address2=}, CustomerDiscounts=$3,698.86, Origin={StateOrProvince=OH, CountryCode=USA, ZipOrPostalCode=44149, City=STRONGSVILLE}, CategorizedMessages={RatesMessages=[FAK 50 APPLIES TO CLASS 50 TO 70], GeneralMessages=[* Not to exceed 20,000lbs or 20'. Shipments over 10,000lbs or over 10' may be delayed for pickup or experience service delays due to current capacity limitations. Please contact your Service Center for more information by calling 800.543.5589., Note: Some residential deliveries may require additional days of service., * This quote is based on information you provide. The actual charges shall be determined by actual shipment characteristics, and any accessorial charges that are applicable to the shipment at time of shipment., For classes higher than 300, contact the Rate department at 800-535-1983., Please note that a sufferance and in-bond storage charges accumulated in the customs process are the responsibility of the debtor of the freight charges., This rate is not applicable to tradeshows. Please contact our Rate Specialist at 800-535-1983 for more information.], TransitMessages=[]}, OriginServiceCenter={Code=NOR, State=OH, ZipCode=44857-9519, Location=Norwalk, OH, Phone=1-800-634-7259, City=Norwalk, Address2=, Address1=1403 St. Rt. 18 E}, ServiceLevels=[{Charge=$3,956.00, QuoteNumber=5555555, ServiceDays=2.0, Name=Standard Service, Code=STD, NetCharge=$378.77, HourlyWindow=null}, {HourlyWindow=null, NetCharge=$430.19, Charge=$51.42, ServiceDays=2.0, Code=GSDS, QuoteNumber=28477953, Name=Guaranteed Service}]}, Errors=[]}
I'm trying to filter out the specific data like the results for the DestinationServiceCenter, OriginServiceCenter, QuoteNumber, ServiceDays, NetCharge, etc.
CodePudding user response:
Just read those values from data
:
const destinationServiceCenter = data.RateQuote.DestinationServiceCenter
const originServiceCenter = data.RateQuote.OriginServiceCenter
const quoteNumber = data.RateQuote.ServiceLevels.map(x => x.QuoteNumber)
const serviceDays = data.RateQuote.ServiceLevels.map(x => x.ServiceDays)
const netCharge = data.RateQuote.ServiceLevels.map(x => x.NetCharge)
const etc // = ????