Structure and Parse GS1 Digital Link URIs

As a web developer, working with GS1 Digital Link URIs involves understanding how to structure them according to GS1 standards and how to parse them effectively to extract useful data. Here’s a guide to help you structure and parse these URIs within your web applications.

1. Understanding the Structure of a GS1 Digital Link URI

A GS1 Digital Link URI encodes product information, such as GTINs, serial numbers, and other identifiers, into a standardized URL format. The structure is both human-readable and machine-readable, allowing it to be used in various digital contexts, like QR codes or direct web links

Basic Structure:

https://example.com/01/gtin/21/serial/10/lot/17/expiry

Components:

  • Domain: https://example.com - The base domain of your service or product information repository.

  • GS1 Application Identifiers (AIs): Numbers like 01, 21, 10, and 17 represent specific types of data.

    • 01 - GTIN (Global Trade Item Number)
    • 21 - Serial Number (optional)
    • 10 - Batch or Lot Number (optional)
    • 17 - Expiration Date (optional)
  • Values: The actual values corresponding to each AI are encoded directly after the AI

Example:

https://mybrand.com/01/12345678901231/21/XYZ123/10/ABCD/17/240101

In this example:

  • 01/12345678901231 - GTIN is 12345678901231
  • 21/XYZ123 - Serial number is XYZ123
  • 10/ABCD - Lot number is ABCD
  • 17/240101 - Expiration date is January 1, 2024

 

2. Building a GS1 Digital Link URI

When constructing a GS1 Digital Link URI, you need to ensure that it conforms to the GS1 syntax and includes the necessary AIs and values relevant to your product.

Steps to Build a URI:

  1. Start with the Base URL: Begin with the base URL that points to your server or the location where the product data is stored.
    https://mybrand.com/
  2. Add the GS1 Identifiers: Append the AIs and their corresponding values to the base URL in the correct order.
    https://mybrand.com/01/12345678901231/21/XYZ123/10/ABCD/17/240101
  3. Optional Parameters: If you need to include additional information like batch numbers or expiry dates, include those as optional parameters in the same format.

Considerations:

  • URL Encoding: Ensure that any special characters in the values are URL-encoded to prevent issues with parsing.
  • HTTPS: Always use HTTPS to ensure the security of the data transmitted via the URI.

 

3. Parsing GS1 Digital Link URIs

To extract and use the data encoded in a GS1 Digital Link URI, you'll need to parse the URI within your application. This typically involves breaking down the URI into its components and extracting the values associated with each AI.

Parsing Example in JavaScript: Here’s how you can parse a GS1 Digital Link URI using JavaScript:

function parseGS1DigitalLink(uri) {
    const parts = uri.split('/');
    const data = {};
    
    // Loop through parts to extract AI and value pairs
    for (let i = 0; i < parts.length; i++) {
        if (i % 2 !== 0) continue; // Skip value, process AI first
        
        const ai = parts[i];
        const value = parts[i + 1];
        
        data[ai] = value;
    }
    
    return data;
}

// Example usage
const uri = "https://mybrand.com/01/12345678901231/21/XYZ123/10/ABCD/17/240101";
const parsedData = parseGS1DigitalLink(uri);

console.log(parsedData);
/*
Output:
{
  "01": "12345678901231",
  "21": "XYZ123",
  "10": "ABCD",
  "17": "240101"
}
*/

Steps to Parse:

  1. Split the URI: Break the URI into parts using the / separator.
  2. Identify AIs and Values: Loop through the parts, using the even-indexed elements as AIs and the odd-indexed elements as values.
  3. Store in a Data Structure: Store the AI-value pairs in an object or another suitable data structure for further processing.

 

4. Using Parsed Data

Once you have parsed the GS1 Digital Link URI, the extracted data can be used in various ways, such as:

  • Database Queries: Use the GTIN or other identifiers to query your product database and retrieve relevant information.
  • API Requests: Pass the parsed data to APIs for further processing, such as fetching product details or verifying batch information.
  • Dynamic Content: Display different content on your website or app based on the parsed values, like showing specific product information based on the GTIN.

Example Use Case:

const productInfo = fetchProductDetails(parsedData['01']);
displayProductInfo(productInfo);

 

5. Error Handling and Validation

It’s crucial to implement error handling to manage scenarios where the URI might be malformed or contain unexpected values.

  • Validation: Before processing, validate that all required AIs are present and that their values are in the correct format.
  • Error Feedback: Provide clear error messages or fallback options if the URI is invalid or incomplete.
if (!parsedData['01']) {
    throw new Error("GTIN is missing from the URI");
}

 


By understanding how to structure and parse GS1 Digital Link URIs, you can effectively integrate this standard into your web applications, enabling dynamic content generation, enhanced product tracking, and improved user experiences. This approach provides both flexibility and control in managing product-related data in a standardized, globally recognized format.