Main site

Sample codes > Importing Data

Importing Data

The following sample code defines custom rate for a carrier, Direct Freight Express, for postal code zones of Australia. The postal code information will be imported from a csv file, which contains the postal codes of 16,146 cities in Australia. This showcases how JsRates handles large data.

Start by creating a module for the data and rate calculator of Direct Freight Express.

  • Head to the Editor page of the app, click the new file button on the editor tab, rename the new module to DirectFreight.js.
  • Paste the following codes to the newly created files and save.

Direct Freight code

const directFreightRates = {
   "zones":["ADEL","ALBU","ALIC","BALL","BRIS","BROK","BUND","CANB","CRNS","DARW","GEEL","GLAD","GOLD","GOSF","HOBA","IPSW","KATH","LAUN","MARY","MCKY","MELB","NEWC","NSW1","NSW2","NSW3","NSW4","WAGGA WAGGA","NSW5","NSW6","BATEMANS BAY","GOULBURN","ULLADULLA","NSW7","NSW8","NT","PERT","QLD1","QLD2","HERVEY BAY","KINGAROY","QLD3","BILOELA","GRACEMERE","PARKHURST","QLD4","SARINA","QLD5","AYR","CHARTERS TOWERS","EDMONTON","IDALIA","INGHAM","QLD6","QLD7","ROMA","QLD8","PORT DOUGLAS","RAVENSHOE","ROCK","ROCKHAMPTON","SA1","GAWLER","SA2","SA3","MOUNT GAMBIER","SUNS","SYDN","TAS","BURNIE","DEVONPORT","INVERMAY","KINGS MEADOWS","KINGSTON","ULVERSTONE","TENN","TOWN","TWHD","TWOO","VIC1","BENDIGO","CRANBOURNE","FOUNTAIN GATE","MELTON","PAKENHAM","SUNBURY","VIC2","SHEPPARTON","VIC3","WA1","GREENFIELDS","HALLS HEAD","MANDURAH","WA2","AUSTRALIND","WA3","ALBANY","GERALDTON","KALGOORLIE","WA4","WA5","BROOME","WOLL","ALIC-SC1","ALIC-SC2","BALL-SC1","BALL-SC2","BALL-SC3","BALL-SC4","BRIS-SC1","BRIS-SC2","BROK-SC1","BROK-SC2","BUND-SC1","BUND-SC2","DARW-SC1","DARW-SC2","DARW-SC3","DARW-SC4","DARW-SC5","GEEL-SC1","GLAD-SC1","GLAD-SC2","HOBA-SC1","KATH-SC1","KINGSTON-SC1","LAUN-SC1","MARY-SC1","MARY-SC2","MARY-SC3","MARY-SC4","MARY-SC5","MARY-SC6","MARY-SC7","MELB-SC1","NSW1-SC1","NSW1-SC2","NSW1-SC3","NSW2-SC1","NSW2-SC2","NSW2-SC3","NSW2-SC4","NSW2-SC5","NSW3-SC1","NSW3-SC2","NSW3-SC3","NSW3-SC4","NSW3-SC5","NSW4-SC1","NSW4-SC2","NSW5-SC1","NSW5-SC2","NSW5-SC3","NSW5-SC4","NSW5-SC5","NSW5-SC6","NSW5-SC7","NSW5-SC8","NSW5-SC9","NSW5-SC10","NSW5-SC11","NSW5-SC12","NSW5-SC13","NSW5-SC14","NSW6-SC1","NSW7-SC1","NSW7-SC2","NSW8-SC1","NSW8-SC2","NSW8-SC3","NSW8-SC4","NSW8-SC5","NSW8-SC6","NSW8-SC7","NT-SC1","NT-SC2","NT-SC3","NT-SC4","PERT-SC1","QLD1-SC1","QLD1-SC2","QLD1-SC3","QLD1-SC4","QLD1-SC5","QLD1-SC6","QLD1-SC7","QLD2-SC1","QLD2-SC2","QLD2-SC3","QLD2-SC4","QLD2-SC5","QLD2-SC6","QLD2-SC7","QLD2-SC8","QLD2-SC9","QLD3-SC1","QLD3-SC2","QLD3-SC3","QLD3-SC4","QLD3-SC5","QLD3-SC6","QLD4-SC1","QLD4-SC2","QLD4-SC3","QLD4-SC4","QLD4-SC5","QLD4-SC6","QLD5-SC1","QLD5-SC2","QLD5-SC3","QLD5-SC4","QLD5-SC5","QLD5-SC6","QLD5-SC7","QLD5-SC8","QLD5-SC9","QLD5-SC10","QLD5-SC11","QLD5-SC12","QLD6-SC1","QLD6-SC2","QLD6-SC3","QLD6-SC4","QLD6-SC5","QLD6-SC6","QLD6-SC7","QLD6-SC8","QLD6-SC9","QLD7-SC1","QLD7-SC2","QLD7-SC3","QLD7-SC4","QLD7-SC5","QLD7-SC6","QLD8-SC1","QLD8-SC2","QLD8-SC3","QLD8-SC4","QLD8-SC5","QLD8-SC6","QLD8-SC7","QLD8-SC8","QLD8-SC9","QLD8-SC10","QLD8-SC11","QLD8-SC12","QLD8-SC13","QLD8-SC14","RAVENSHOE-SC1","ROCK-SC1","SA1-SC1","SA1-SC2","SA1-SC3","SA2-SC1","SA2-SC2","SA3-SC1","SA3-SC2","SA3-SC3","SA3-SC4","SA3-SC5","SA3-SC6","SA3-SC7","SYDN-SC1","TENN-SC1","VIC1-SC1","VIC1-SC2","VIC1-SC3","VIC1-SC4","VIC1-SC5","VIC1-SC6","VIC1-SC7","VIC2-SC1","VIC2-SC2","VIC2-SC3","VIC2-SC4","VIC2-SC5","VIC2-SC6","VIC2-SC7","VIC3-SC1","VIC3-SC2","VIC3-SC3","VIC3-SC4","VIC3-SC5","VIC3-SC6","VIC3-SC7","VIC3-SC8","VIC3-SC9","WA1-SC1","WA1-SC2","WA3-SC1","WA4-SC1","WA4-SC2","WA4-SC3","WA5-SC1","WA5-SC2","WA5-SC3","WA5-SC4","WA5-SC5"],
   "fixed":[21.58,23.40,21.61,22.34,21.58,21.93,21.93,21.58,20.64,21.61,22.34,21.93,23.40,21.58,21.93,21.58,19.50,21.93,21.93,21.93,22.34,21.58,21.93,21.93,21.93,21.93,23.40,21.93,21.93,23.40,23.40,23.40,22.96,22.96,22.53,21.93,21.93,21.93,21.93,21.93,20.64,21.93,21.93,21.93,20.64,21.93,23.85,20.64,20.64,20.64,21.93,21.93,23.85,23.85,22.96,22.53,21.61,21.61,21.93,21.93,22.96,23.40,21.93,21.93,24.49,21.93,21.58,25.34,21.93,23.40,21.93,21.93,21.93,21.93,22.53,21.93,21.93,21.93,23.12,22.34,22.34,22.34,22.34,22.34,22.34,23.12,23.12,21.58,20.64,21.93,21.93,21.93,22.25,20.64,22.53,22.25,22.25,22.25,22.53,22.53,22.53,21.58,106.88,137.88,59.99,69.40,78.82,88.23,25.44,548.70,104.30,170.19,79.59,104.30,176.64,254.16,2,347.14,60.36,83.62,59.99,2,492.81,71.35,153.71,2,215.83,38.41,153.71,54.88,63.11,71.35,104.30,120.77,137.24,145.48,26.48,54.88,63.11,71.35,38.41,46.64,54.88,63.11,71.35,46.64,293.73,54.88,63.11,79.59,46.64,63.11,46.64,269.02,54.88,71.35,79.59,87.82,680.83,96.06,104.30,112.53,120.77,129.00,137.24,145.48,63.11,47.66,64.14,47.66,401.82,72.37,517.13,105.32,121.79,171.21,168.95,242.16,81.10,103.06,145.48,2,492.81,54.88,63.11,71.35,79.59,137.24,145.48,54.88,63.11,71.35,79.59,87.82,94.41,104.30,137.24,145.48,51.65,59.40,67.15,98.16,129.17,136.92,206.69,59.40,67.15,74.91,113.66,129.17,178.89,47.11,256.41,54.86,62.61,70.36,78.11,85.87,93.62,101.37,116.87,124.63,178.89,54.86,62.61,70.36,85.87,116.87,126.18,140.13,147.88,62.61,70.36,78.11,85.87,101.37,140.13,242.16,2,218.86,249.48,59.13,388.58,73.77,549.65,81.10,88.42,95.74,901.06,125.02,132.34,139.66,75.87,2,492.81,39.43,47.66,146.50,46.64,145.48,38.41,46.64,301.97,54.88,63.11,104.30,145.48,28.25,125.02,41.94,51.36,60.77,70.18,79.60,89.01,98.42,51.36,60.77,70.18,89.01,98.42,117.25,136.07,47.93,56.72,65.50,460.84,74.29,83.07,100.64,109.43,127.00,82.66,113.66,81.10,212.88,81.10,1,193.90,212.88,81.10,110.38,132.34,139.66],
   "variable":[0.45,0.45,1.85,0.39,0.73,1.36,1.19,0.45,1.68,2.01,0.29,1.36,0.82,0.65,1.11,0.85,2.20,0.94,1.19,1.45,0.29,0.73,0.94,1.02,1.11,0.85,0.55,1.11,1.11,0.87,0.85,0.87,1.28,1.45,5.30,1.36,1.19,1.62,1.11,1.11,1.68,1.62,1.28,1.28,1.68,1.45,1.68,1.68,1.85,1.70,1.45,1.62,2.17,2.17,1.53,4.17,1.85,1.85,1.33,1.33,0.85,0.73,1.19,1.53,0.82,1.11,0.45,1.62,0.85,0.82,0.94,1.11,1.11,1.02,2.27,1.53,0.85,1.11,0.39,0.39,0.29,0.29,0.29,0.29,0.29,0.47,0.39,0.49,1.85,1.62,1.62,1.53,2.01,1.68,2.50,2.01,2.01,2.01,2.96,4.85,2.96,0.64,1.85,1.85,0.39,0.39,0.39,0.39,0.73,0.73,1.36,1.36,1.19,1.19,2.01,2.01,2.01,2.01,2.01,0.29,1.36,1.36,1.11,2.20,1.11,0.94,1.19,1.19,1.19,1.19,1.19,1.19,1.19,0.29,0.94,0.94,0.94,1.02,1.02,1.02,1.02,1.02,1.11,1.11,1.11,1.11,1.11,0.85,0.85,1.11,1.11,1.11,1.11,1.11,1.11,1.11,1.11,1.11,1.11,1.11,1.11,1.11,1.11,1.11,1.28,1.28,1.45,1.45,1.45,1.45,1.45,1.45,1.45,5.30,5.30,5.30,5.30,1.36,1.19,1.19,1.19,1.19,1.19,1.19,1.19,1.62,1.62,1.62,1.62,1.62,1.62,1.62,1.62,1.62,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,1.68,2.17,2.17,2.17,2.17,2.17,2.17,2.17,2.17,2.17,2.17,2.17,2.17,2.17,2.17,2.17,4.17,4.17,4.17,4.17,4.17,4.17,4.17,4.17,4.17,4.17,4.17,4.17,4.17,4.17,1.85,1.33,0.85,0.85,0.85,1.19,1.19,1.53,1.53,1.53,1.53,1.53,1.53,1.53,0.45,2.27,0.39,0.39,0.39,0.39,0.39,0.39,0.39,0.47,0.47,0.47,0.47,0.47,0.47,0.47,0.49,0.49,0.49,0.49,0.49,0.49,0.49,0.49,0.49,1.85,1.85,2.50,2.96,2.96,2.96,4.85,4.85,4.85,4.85,4.85],
   "minimumCharge":[20.00,20.91,18.45,18.51,20.00,19.61,19.61,20.91,18.45,18.45,18.51,19.61,20.91,20.91,19.61,20.91,17.43,19.61,19.61,19.61,18.51,20.91,19.61,19.61,19.61,19.61,20.91,19.61,19.61,20.91,20.91,20.91,19.61,19.61,17.43,19.61,19.61,19.61,19.61,19.61,18.45,19.61,19.61,19.61,18.45,19.61,18.45,18.45,18.45,18.45,19.61,19.61,18.45,18.45,19.61,17.43,18.45,18.45,19.61,19.61,19.61,20.91,19.61,19.61,20.91,19.61,20.00,19.61,19.61,20.91,19.61,19.61,19.61,19.61,17.43,19.61,19.61,19.61,18.51,18.51,18.51,18.51,18.51,18.51,18.51,20.46,20.46,20.91,18.45,19.61,19.61,19.61,18.45,18.45,17.43,18.45,18.45,18.45,17.43,17.43,17.43,20.91,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

}

const directFreightInterval ={
    "weightUpper":[1.99,3.99,5.99,7.99,9.99,11.99,13.99,15.99,17.99,19.99,24.99,29.99,34.99,39.99,44.99,49.99,54.99,59.99,64.99,69.99,74.99,79.99,84.99,89.99,94.99,99.99,109.99,119.99,129.99,139.99,149.99,159.99,169.99,179.99,189.99,199.99],
    "weightLower":[0,2,4,6,8,10,12,14,16,18,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,110,120,130,140,150,160,170,180,190]
}

export const getDirectFreightRates = (postcode,weight) =>{
    const index = directFreightInterval.weightLower.findLastIndex((lower) => lower <= weight );
    const G4 = directFreightInterval.weightLower[index];
    const G5 = directFreightInterval.weightUpper[index];
    
    const index_postcode =  directFreightZones.postcode.indexOf(postcode.toString());
    const zone =  directFreightZones.zones[index_postcode];
    
    const index_zone = directFreightRates.zones.indexOf(zone);
     
    const C6 = directFreightRates.fixed[index_zone];
    const D6 = directFreightRates.variable[index_zone];
    const E6 = directFreightRates.minimumCharge[index_zone]; 
    
    const average = (G4 + G5) / 2;
    const value = C6 + average * D6;
    return value < E6 ? E6 : value;
}

Importing data

  • Download the csv file containing the postal code information from https://files.jsrates.com/directFreightZones.csv
  • Import the data to a new file, by clicking More actions, and selecting Import from the dropdown menu.
  • Add the data in the csv file you downloaded. This will import the data in a new editor file.
  • Copy the code from the new editor file, paste at the top of the DirectFreight.js module you created previously, and save it. Make sure the copied code has a variable name directFreightZones containing a JavaScript object with key postcode, city, and zone.
  • Delete the new file created from the import, by clicking More actions, and selecting Delete file from the dropdown menu.

calculateShippingRates code

Copy the following code and paste it to a blank calculateShippingRates.js module and save it.

import { getDirectFreightRates } from './DirectFreight.js';

export async function calculateShippingRates(DATA, env) {

    const postalCode = parseInt((DATA.destination.postal_code).trim(),0);
    let rate = 0;

    DATA.items.forEach(item => {
        const itemWeight = item.grams / 1000;
        rate += getDirectFreightRates(postalCode,itemWeight);
    });
    
   const rates =[{
        service_name: "Direct Express",
        service_code: "DIRECTEXPRESS",
        total_price: Math.round(rate * 100).toString(),
        currency: "AUD",
    }];

    return { rates };
}

The following video demonstrates how to import the data.