Skip to Main Content
Cognex Support Hub
LogoLogo

Script - JSON Formating of Output Data

Short example of how to format a sample JSON data output.

01.01.1

Details

In today’s world, there’s a need to send larger amounts of data in a single message. JSON formatting makes this process much easier.

 

JSON (JavaScript Object Notation) is a lightweight, text-based format used for storing and exchanging data. It consists of key-value pairs, where keys are strings and values can be various data types, including numbers, strings, and arrays. JSON is easy for humans to read and write, and for machines to parse and generate. 

 

To store the data we will be using a  structures. A data object is a way to store and organize data using key-value pairs. It’s one of the most fundamental and useful structures in the language. 

A JavaScript object is defined using curly braces {}. Inside, you define properties in the form of key: value pairs.

const person = {
 name: "Alice",
 age: 30,
 isStudent: false
};

  • name, age, and isStudent are the keys (or property names).
  • "Alice", 30, and false are the values.

 

DataMan systems can utilize JavaScript formatting to send such messages. Below is a sample script that accomplishes this task:

 

'use strict'; // Enforces strict mode to catch common coding errors and prevent unsafe actions

const NO_READ = 'No read!'; // Constant used to indicate a failed or absent read

// Main function that processes the decoding result
function onResult(decodeResults, readerProperties, output) {
 let outputString = NO_READ; // Default output string set to "No read!"

 // Check if the first decode result is valid (i.e., successfully decoded)
 if (decodeResults[0].decoded) {
   let decodedCode = decodeResults[0].content; // Extract the decoded content (e.g., barcode or QR code)
   let triggerId = readerProperties.trigger.index; // Get the trigger index from reader properties
   let deviceName = decodeResults[0].source; // Get the name or ID of the scanning device

   // Construct the data object with various metadata fields
   const codeData = {
     readerConex0100: {
       header: {
         messageType: "stationData",      // Type of message being sent
         messageVersion: "0100",          // Version of the message structure
         senderId: deviceName,            // ID or name of the device that sent the message
         siteId: "Side-01",               // Identifier for the location/site
         clientId: "Test"                 // Identifier for the client (can be dynamic)
       },
       repackConfirmation: [
         {
           containerId: decodedCode,      // ID of the container (from the scan)
           containerType: "EUR6",         // Type of the container (e.g., pallet type)
           triggerID: triggerId,          // ID of the trigger event
           containerStatus: "Open",       // Status of the container
           locationId: "AZONE",           // Location identifier
         }
       ]
     }
   };

   // Convert the constructed object to a JSON string for output
   outputString = JSON.stringify(codeData);
 }

 // Assign the final output string to the output object
 output.content = outputString;
}

Loading component...

Loading component...

Verwandte Materialien

Loading component...