Using scripts to load Label Printing Data

You can use a script to load your data into Label Printing Data.

To do this, in the data load settings, select the source type JavaScript, Pascal and click Edit script.

Below are some examples of scripts for different tasks.

Loading a JSON from the Internet

Example of data to be loaded:

{
    "dataSource": {
        "readyData": [
            {
                "NumberOfLabelsToPrint": 2,
                "SKU": "AZL10002",
                "Name": "Lite, yearly subscription",
                "Brand": "AzureLabel",
                "Barcode": "2345678901234",
                "Price": 45,
                "PriceOld": 49,
                "Description": "Advanced Capabilities",
                "Code": "lite-y",
                "Size": "M",
                "Color": "light azure",
                "Country": "USA",
                "Unit": "Each",
                "Link": "https://azurelabel.com/purchase"
            },
            {
                "NumberOfLabelsToPrint": 3,
                "SKU": "AZL10005",
                "Name": "Pro, yearly subscription",
                "Brand": "AzureLabel",
                "Barcode": "5678901234562",
                "Price": 70,
                "PriceOld": 79,
                "Description": "Professional Power",
                "Code": "pro-y",
                "Size": "L",
                "Color": "azure",
                "Country": "USA",
                "Unit": "Each",
                "Link": "https://azurelabel.com/purchase"
            }
        ]
    }
}

Solution

JavaScript

// JSON test data url
const url = 'https://azurelabel.com/docs/resources/script-guide/text/example001.json';

// Send a request to the URL. The response is an array of two strings: the server response code and the test data (JSON)
const response = azureLabel.httpRequest( url, 'get', null, null, null, null, null, null, true, 1, 10000, 10000, false, null, null);

// Convert the JSON string into an array
const data = JSON.parse(response[1]);

// The AzureLabel library is required to use the special ListOfRecords class
const ext = require("azurelabel_ext");
// Create a list of records
const list = new ext.ListOfRecords;
// Add data from the JSON array to the list. We use only the element named "readyData"
let rowIndex = 0;
data['dataSource']['readyData'].forEach(row => {
    // Add to the list a new record
    list.add();
    Object.keys(row).forEach(key => {
        // Add a field to the record. Field name is the JSON object name. Field value is the JSON object value
        list.setFieldValue(rowIndex, key, row[key]);
    });
    rowIndex++;
});

// Clear the "Label Printing Data" and add data from the list
azureLabel.setLoadedData(list);