### Sample EDI Document for SimpleOrder Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/ballerina/README.md An example EDI document conforming to the SimpleOrder schema. It includes header and item segments with their respective fields. ```edi HDR*ORDER_1201*ABC_Store*2008-01-01~ ITM*A-250*12~ ITM*A-45*100~ ITM*D-10*58~ ITM*K-80*250~ ITM*T-46*28~ ``` -------------------------------- ### Get EDI Schema from JSON File Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/docs/specs/ModuleSpecification.md Reads an EDI schema from a JSON file and converts it into a Ballerina `EdiSchema` type. This function requires the `ballerina/io` and `balarina/edi` modules. ```ballerina import ballerina/io; import balarina/edi; public function main() returns error? { edi:EdiSchema schema = check edi:getSchema(check io:fileReadJson("resources/edi-schema.json")); io:println(schema.toString()); } ``` -------------------------------- ### Publish Ballerina EDI Artifacts to Local Repository Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/README.md Execute this command to build the package and publish the generated artifacts to the local Ballerina Central repository. ```bash ./gradlew clean build -PpublishToLocalCentral=true ``` -------------------------------- ### Build Ballerina EDI Package Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/README.md Execute this command to clean the build artifacts and compile the package. ```bash ./gradlew clean build ``` -------------------------------- ### Configure EDI Processing Options Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/docs/specs/SchemaSpecification.md Set up optional configurations for EDI processing, such as delimiters, ignored segments, and preservation of empty fields. ```json { "name": "SimpleOrder", "delimiters" : {"segment" : "~", "field" : "*", "component": ":", "repetition": "^"}, "ignoreSegments": ["UNA", "IGN", "UNZ"], "preserveEmptyFields": true, "includeSegmentCode": true, "segments" : [ { "code": "HDR", "tag" : "header", "fields" : [{"tag": "code"}, {"tag" : "orderId"}, {"tag" : "organization"}, {"tag" : "date"}] }, { "code": "ITM", "tag" : "items", "maxOccurances" : -1, "fields" : [{"tag": "code"}, {"tag" : "item"}, {"tag" : "quantity", "dataType" : "int"}] } ] } ``` -------------------------------- ### EDI Parsing and Serialization with Error Handling Source: https://context7.com/ballerina-platform/module-ballerina-edi/llms.txt Demonstrates loading an EDI schema, parsing an EDI string to JSON, and serializing JSON back to an EDI string. Includes precise error handling for schema loading, parsing, and serialization failures using `edi:Error`. ```ballerina import ballerina/io; import ballerina/edi; public function main() returns error? { json schemaJson = check io:fileReadJson("resources/schema.json"); edi:EdiSchema|error schemaResult = edi:getSchema(schemaJson); if schemaResult is edi:Error { io:println("Schema loading failed: " + schemaResult.message()); return; } edi:EdiSchema schema = schemaResult; string ediText = check io:fileReadString("resources/sample.edi"); json|edi:Error parseResult = edi:fromEdiString(ediText, schema); if parseResult is edi:Error { io:println("EDI parsing failed: " + parseResult.message()); return; } json orderData = parseResult; string|edi:Error writeResult = edi:toEdiString(orderData, schema); if writeResult is edi:Error { io:println("EDI serialization failed: " + writeResult.message()); return; } io:println(writeResult); } ``` -------------------------------- ### Run Tests for Ballerina EDI Package Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/README.md Execute this command to clean the build artifacts and run all defined tests. ```bash ./gradlew clean test ``` -------------------------------- ### Build Ballerina EDI Package Without Tests Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/README.md Use this command to perform a clean build of the package, excluding test execution. ```bash ./gradlew clean build -x test ``` -------------------------------- ### Publish Ballerina EDI Artifacts to Ballerina Central Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/README.md Use this command to build the package and publish the generated artifacts to the main Ballerina Central repository. ```bash ./gradlew clean build -PpublishToCentral=true ``` -------------------------------- ### Debug Ballerina EDI Package with Ballerina Language Debugger Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/README.md Build the package with support for debugging using the Ballerina language debugger on a specified port. ```bash ./gradlew clean build -PbalJavaDebug= ``` -------------------------------- ### Debug Ballerina EDI Package with Remote Debugger Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/README.md Build the package while enabling remote debugging on a specified port. ```bash ./gradlew clean build -Pdebug= ``` -------------------------------- ### Read and Parse EDI File in Ballerina Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/ballerina/README.md Reads an EDI file, parses it using a defined schema, and converts it to JSON. Ensure the schema and EDI files are correctly placed in the 'resources' directory. ```ballerina import ballerina/io; import ballerina/edi; public function main() returns error? { // Step 1: Load EDI schema from a JSON file edi:EdiSchema schema = check edi:getSchema(check io:fileReadJson("resources/schema.json")); // Step 2: Read the EDI file as a string string ediText = check io:fileReadString("resources/sample.edi"); // Step 3: Convert EDI string to JSON using the specified schema json orderData = check edi:fromEdiString(ediText, schema); // Step 4: Print the resulting JSON data io:println(orderData.toJsonString()); } ``` -------------------------------- ### Define Fields with Data Types and Lengths Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/docs/specs/SchemaSpecification.md Use this to define fields with their tags, data types, and specific length constraints, including composite types. ```json "fields": [ {"tag": "CustomerName", "dataType": "string", "length": 50}, {"tag": "Quantity", "dataType": "int", "length": {"min": 1}}, {"tag": "Price", "dataType": "float", "length": {"max": 10}}, {"tag": "Address", "dataType": "composite", "components": [ { "tag": "No", "required": false, "dataType": "string" }, { "tag": "Street", "required": false, "dataType": "string" }, { "tag": "City", "required": false, "dataType": "string" } ]} ] ``` -------------------------------- ### `toEdiString` — Serialize JSON into EDI text Source: https://context7.com/ballerina-platform/module-ballerina-edi/llms.txt Converts a JSON map into a well-formed EDI string according to the supplied `EdiSchema`. Each key in the JSON map is matched to the corresponding segment tag in the schema, fields are serialized in order using the schema's delimiters, and segment lines are terminated with the segment delimiter. Array values are written as multiple segment lines. Returns an `edi:Error` if the JSON structure is incompatible with the schema (e.g., a non-map value is passed, or a required field is missing). ```APIDOC ## `toEdiString` — Serialize JSON into EDI text Converts a JSON map into a well-formed EDI string according to the supplied `EdiSchema`. Each key in the JSON map is matched to the corresponding segment tag in the schema, fields are serialized in order using the schema's delimiters, and segment lines are terminated with the segment delimiter. Array values are written as multiple segment lines. Returns an `edi:Error` if the JSON structure is incompatible with the schema (e.g., a non-map value is passed, or a required field is missing). ```ballerina import ballerina/io; import ballerina/edi; public function main() returns error? { edi:EdiSchema schema = check edi:getSchema(check io:fileReadJson("resources/schema.json")); json order = { "header": { "code": "HDR", "orderId": "ORDER_1201", "organization": "ABC_Store", "date": "2008-01-01" }, "items": [ {"code": "ITM", "item": "A-250", "quantity": 12}, {"code": "ITM", "item": "B-250", "quantity": 10}, {"code": "ITM", "item": "C-100", "quantity": 5} ] }; string ediOutput = check edi:toEdiString(order, schema); io:println(ediOutput); // Output: // HDR*ORDER_1201*ABC_Store*2008-01-01~ // ITM*A-250*12~ // ITM*B-250*10~ // ITM*C-100*5~ } ``` ``` -------------------------------- ### toEdiString function Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/docs/specs/ModuleSpecification.md Writes the given JSON data into EDI text format according to the provided schema. Returns an error if the writing process fails. ```APIDOC ## toEdiString function ### Description Writes the given JSON variable into EDI text according to the provided schema. ### Function Signature ```ballerina function toEdiString(json msg, EdiSchema schema) returns string|Error ``` ### Parameters #### Parameters - **msg** (json) - JSON value to be written into EDI. - **schema** (EdiSchema) - Schema of the EDI text. ### Return Type - **string|Error** - EDI text containing the data provided in the JSON variable. Error if the writing fails. ``` -------------------------------- ### Parse Fixed-Length EDI using Ballerina Source: https://context7.com/ballerina-platform/module-ballerina-edi/llms.txt Parses a fixed-length EDI string into a JSON payload using a predefined EDI schema. Ensure the schema file and EDI data file are correctly placed in the resources directory. ```ballerina import ballerina/io; import ballerina/edi; public function main() returns error? { edi:EdiSchema schema = check edi:getSchema(check io:fileReadJson("resources/fixed-schema.json")); string fixedEdi = check io:fileReadString("resources/shipment.edi"); json shipmentData = check edi:fromEdiString(fixedEdi, schema); io:println(shipmentData.toJsonString()); } ``` -------------------------------- ### Load EDI Schema from JSON Source: https://context7.com/ballerina-platform/module-ballerina-edi/llms.txt Loads an EDI schema from a JSON file or an inline JSON literal. The schema is validated and denormalized for use with translation functions. Ensure the JSON file path is correct. ```ballerina import ballerina/io import ballerina/edi public function main() returns error? // Load schema from a JSON file on disk json schemaJson = check io:fileReadJson("resources/schema.json") edi:EdiSchema schema = check edi:getSchema(schemaJson) // Alternatively, pass an inline JSON literal json inlineSchema = { "name": "SimpleOrder", "delimiters": {"segment": "~", "field": "*", "component": ":", "repetition": "^"}, "segments": [ { "code": "HDR", "tag": "header", "minOccurances": 1, "fields": [ {"tag": "code"}, {"tag": "orderId", "required": true}, {"tag": "organization"}, {"tag": "date"} ] }, { "code": "ITM", "tag": "items", "maxOccurances": -1, "fields": [ {"tag": "code"}, {"tag": "item", "required": true}, {"tag": "quantity", "dataType": "int", "required": true} ] } ] } edi:EdiSchema schemaFromLiteral = check edi:getSchema(inlineSchema) io:println(schemaFromLiteral.name) // Output: SimpleOrder ``` -------------------------------- ### Convert JSON to EDI String Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/docs/specs/ModuleSpecification.md Use this function to convert JSON data to an EDI string format. Ensure the EDI schema is loaded correctly before conversion. ```ballerina import ballerina/io; import balarinax/edi; public function main() returns error? { json order2 = {...}; edi:EdiSchema schema = check edi:getSchema(check io:fileReadJson("resources/edi-schema.json")); string orderEDI = check edi:toEdiString(order2, schema); io:println(orderEDI); } ``` -------------------------------- ### getSchema function Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/docs/specs/ModuleSpecification.md Creates an EDI schema from a string or a JSON. It takes a schema definition and returns an EdiSchema record or an error if the schema is invalid. ```APIDOC ## getSchema function ### Description Creates an EDI schema from a string or a JSON. ### Function Signature ```ballerina function getSchema(string|json schema) returns EdiSchema|error ``` ### Parameters #### Parameters - **schema** (string|json) - Schema of the EDI type. ### Return Type - **EdiSchema|error** - Error is returned if the given schema is not valid. ### Example The following is a basic EDI schema, assuming it is stored in the `edi-schema.json` file: ```json { "name": "SimpleOrder", "delimiters" : {"segment" : "~", "field" : "*", "component": ":", "repetition": "^"}, "segments" : [ { "code": "HDR", "tag" : "header", "fields" : [{"tag": "code"}, {"tag" : "orderId"}, {"tag" : "organization"}, {"tag" : "date"}] }, { "code": "ITM", "tag" : "items", "maxOccurances" : -1, "fields" : [{"tag": "code"}, {"tag" : "item"}, {"tag" : "quantity", "dataType" : "int"}] } ] } ``` Below code reads the edi-schema.json file and assign to a edi:EdiSchema variable which holds Ballerina EDI Schema. ```ballerina import ballerina/io; import balarina/edi; public function main() returns error? { edi:EdiSchema schema = check edi:getSchema(check io:fileReadJson("resources/edi-schema.json")); io:println(schema.toString()); } ``` ``` -------------------------------- ### Define Fields with Length Constraints Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/docs/specs/SchemaSpecification.md Specify length constraints for fields, including fixed lengths, minimum lengths, maximum lengths, or a combination. ```json "fields": [ {"tag": "DocumentNameCode", "length": 10}, {"tag": "DocumentNumber", "length": {"min": 1}}, {"tag": "MessageFunction", "length": {"max": 3}}, {"tag": "ResponseType", "length": {"min": 1, "max": 3}} ] ``` -------------------------------- ### Define Sub-components within a Component Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/docs/specs/SchemaSpecification.md Define sub-components within a component, including their tags, requirements, and data types. This allows for deeper nesting. ```json "code": "ORG", "tag": "organization", "fields": [ {"tag": "code"},{"tag": "partnerCode"},{"tag": "name"},{"tag": "contact", "components": [ {"tag": "mobile", "required": true}, {"tag": "fixedLine"}, {"tag": "address", "subcomponents": [{"tag": "streetAddress"},{"tag": "city"},{"tag": "country"}]} ] } ] ``` -------------------------------- ### fromEdiString function Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/docs/specs/ModuleSpecification.md Reads the given EDI text according to the provided schema and converts it into a JSON object. Returns an error if the reading process fails. ```APIDOC ## fromEdiString function ### Description Reads the given EDI text according to the provided schema. ### Function Signature ```ballerina function fromEdiString(string ediText, EdiSchema schema) returns json|Error ``` ### Parameters #### Parameters - **ediText** (string) - EDI text to be read. - **schema** (EdiSchema) - Schema of the EDI text. ### Return Type - **json|Error** - JSON variable containing EDI data. Error if the reading fails. ### Example Below code reads the `edi-sample.edi` into a json variable named "orderData". _(given schema is based on the schema used in the above example)_ ```ballerina import ballerina/io; import balarina/edi; public function main() returns error? { edi:EdiSchema schema = check edi:getSchema(check io:fileReadJson("resources/edi-schema.json")); string ediText = check io:fileReadString("resources/edi-sample.edi"); json orderData = check edi:fromEdiString(ediText, schema); io:println(orderData.toJsonString()); } ``` ``` -------------------------------- ### Serialize JSON into EDI text using `toEdiString` Source: https://context7.com/ballerina-platform/module-ballerina-edi/llms.txt Use `toEdiString` to convert a JSON map into an EDI formatted string according to an `EdiSchema`. It serializes fields in order, uses the schema's delimiters, and writes array values as multiple segment lines. Returns an `edi:Error` for schema incompatibilities. ```ballerina import ballerina/io; import ballerina/edi; public function main() returns error? { edi:EdiSchema schema = check edi:getSchema(check io:fileReadJson("resources/schema.json")); json order = { "header": { "code": "HDR", "orderId": "ORDER_1201", "organization": "ABC_Store", "date": "2008-01-01" }, "items": [ {"code": "ITM", "item": "A-250", "quantity": 12}, {"code": "ITM", "item": "B-250", "quantity": 10}, {"code": "ITM", "item": "C-100", "quantity": 5} ] }; string ediOutput = check edi:toEdiString(order, schema); io:println(ediOutput); // Output: // HDR*ORDER_1201*ABC_Store*2008-01-01~ // ITM*A-250*12~ // ITM*B-250*10~ // ITM*C-100*5~ } ``` -------------------------------- ### Define Components within a Field Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/docs/specs/SchemaSpecification.md Structure nested components within a field, specifying tags, requirements, and data types for each component. ```json "code": "ORG", "tag": "organization", "fields": [{"tag": "code"},{"tag": "partnerCode"},{"tag": "name"}, { "tag": "address", "components": [ {"tag": "streetAddress"}, {"tag": "city"}, {"tag": "country"} ] }, { "tag": "contact", "repeat": true } ] ``` -------------------------------- ### Convert EDI Text to JSON Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/docs/specs/ModuleSpecification.md Parses a given EDI text string into a JSON object based on a provided `EdiSchema`. This function requires the `ballerina/io` and `balarina/edi` modules, and assumes the schema and EDI text are available as files. ```ballerina import ballerina/io; import balarina/edi; public function main() returns error? { edi:EdiSchema schema = check edi:getSchema(check io:fileReadJson("resources/edi-schema.json")); string ediText = check io:fileReadString("resources/edi-sample.edi"); json orderData = check edi:fromEdiString(ediText, schema); io:println(orderData.toJsonString()); } ``` -------------------------------- ### `fromEdiString` — Parse EDI text into JSON Source: https://context7.com/ballerina-platform/module-ballerina-edi/llms.txt Reads an EDI-formatted string and converts it into a JSON value following the structure dictated by the provided `EdiSchema`. Segments are matched to their schema definitions by code; each segment's fields, components, and sub-components are extracted and placed into a nested JSON map. Segments declared with `maxOccurances: -1` (unlimited) are represented as JSON arrays. Returns an `edi:Error` if the EDI text violates the schema constraints (e.g., a required segment is missing or a field fails a length constraint). ```APIDOC ## `fromEdiString` — Parse EDI text into JSON Reads an EDI-formatted string and converts it into a JSON value following the structure dictated by the provided `EdiSchema`. Segments are matched to their schema definitions by code; each segment's fields, components, and sub-components are extracted and placed into a nested JSON map. Segments declared with `maxOccurances: -1` (unlimited) are represented as JSON arrays. Returns an `edi:Error` if the EDI text violates the schema constraints (e.g., a required segment is missing or a field fails a length constraint). ```ballerina import ballerina/io; import ballerina/edi; public function main() returns error? { edi:EdiSchema schema = check edi:getSchema(check io:fileReadJson("resources/schema.json")); // EDI text: one header segment + five item segments string ediText = check io:fileReadString("resources/sample.edi"); // Content of sample.edi: // HDR*ORDER_1201*ABC_Store*2008-01-01~ // ITM*A-250*12~ // ITM*A-45*100~ // ITM*D-10*58~ // ITM*K-80*250~ // ITM*T-46*28~ json orderData = check edi:fromEdiString(ediText, schema); io:println(orderData.toJsonString()); // Output: // { // "header": {"code":"HDR","orderId":"ORDER_1201","organization":"ABC_Store","date":"2008-01-01"}, // "items": [ // {"code":"ITM","item":"A-250","quantity":12}, // {"code":"ITM","item":"A-45","quantity":100}, // {"code":"ITM","item":"D-10","quantity":58}, // {"code":"ITM","item":"K-80","quantity":250}, // {"code":"ITM","item":"T-46","quantity":28} // ] // } // Access specific fields json header = check orderData.header; string orderId = check header.orderId; io:println(orderId); // Output: ORDER_1201 } ``` ``` -------------------------------- ### Define EDI Schema using JSON Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/docs/specs/ModuleSpecification.md Defines a basic EDI schema structure in JSON format, including delimiters and segment definitions with fields. This schema can be used to configure EDI parsing and serialization. ```json { "name": "SimpleOrder", "delimiters" : {"segment" : "~", "field" : "*", "component": ":", "repetition": "^"}, "segments" : [ { "code": "HDR", "tag" : "header", "fields" : [{"tag": "code"}, {"tag" : "orderId"}, {"tag" : "organization"}, {"tag" : "date"}] }, { "code": "ITM", "tag" : "items", "maxOccurances" : -1, "fields" : [{"tag": "code"}, {"tag" : "item"}, {"tag" : "quantity", "dataType" : "int"}] } ] } ``` -------------------------------- ### Define EDI Delimiters Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/docs/specs/SchemaSpecification.md Specifies the characters used to separate segments, fields, components, and repetitions within EDI data. Ensure these match the expected EDI file format. ```json { "delimiters": { "segment": "~", "field": "*", "component": ":", "repetition": "^" } } ``` -------------------------------- ### Define EDI Segments and Fields Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/docs/specs/SchemaSpecification.md Defines the structure of EDI segments, including their code, tag, occurrence constraints, and the fields they contain. Fields can have tags, be required, and specify data types. ```json { "segments": [ { "code": "HDR", "tag": "header", "minOccurances": 1, "maxOccurances": 1, "fields": [ {"tag": "code", "required": true}, {"tag": "orderId", "required": true}, {"tag": "organization"}, {"tag": "date"} ] }, { "code": "ITM", "tag": "items", "minOccurances": 0, "maxOccurances": -1, "fields": [ {"tag": "code", "required": true}, {"tag": "item", "required": true}, {"tag": "quantity", "required": true, "dataType": "int"} ] } ] } ``` -------------------------------- ### getSchema - Load and validate an EDI schema Source: https://context7.com/ballerina-platform/module-ballerina-edi/llms.txt Parses a JSON object or string into an EdiSchema record, validating its structure and resolving references. This schema can then be reused for EDI to JSON and JSON to EDI conversions. ```APIDOC ## getSchema - Load and validate an EDI schema ### Description Parses a JSON object or JSON string into an `EdiSchema` record that can be reused across multiple `fromEdiString` and `toEdiString` calls. The function validates the schema structure, resolves any `segmentDefinitions` references (denormalization), and returns an error if the schema is malformed. The returned `EdiSchema` is immutable-safe to pass to the isolated translation functions. ### Method ```ballerina public function getSchema(json schemaJson | string schemaString) returns edi:EdiSchema|error ``` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **schemaJson | schemaString** (json | string) - Required - The JSON object or string representing the EDI schema. ### Request Example ```ballerina import ballerina/io; import ballerina/edi; public function main() returns error? { // Load schema from a JSON file on disk json schemaJson = check io:fileReadJson("resources/schema.json"); edi:EdiSchema schema = check edi:getSchema(schemaJson); // Alternatively, pass an inline JSON literal json inlineSchema = { "name": "SimpleOrder", "delimiters": {"segment": "~", "field": "*", "component": ":", "repetition": "^"}, "segments": [ { "code": "HDR", "tag": "header", "minOccurances": 1, "fields": [ {"tag": "code"}, {"tag": "orderId", "required": true}, {"tag": "organization"}, {"tag": "date"} ] }, { "code": "ITM", "tag": "items", "maxOccurances": -1, "fields": [ {"tag": "code"}, {"tag": "item", "required": true}, {"tag": "quantity", "dataType": "int", "required": true} ] } ] }; edi:EdiSchema schemaFromLiteral = check edi:getSchema(inlineSchema); io:println(schemaFromLiteral.name); // Output: SimpleOrder } ``` ### Response #### Success Response (200) - **edi:EdiSchema** - A validated and denormalized EDI schema record. #### Response Example ```json { "name": "SimpleOrder", "delimiters": {"segment": "~", "field": "*", "component": ":", "repetition": "^"}, "segments": [ { "code": "HDR", "tag": "header", "minOccurances": 1, "fields": [ {"tag": "code"}, {"tag": "orderId", "required": true}, {"tag": "organization"}, {"tag": "date"} ] }, { "code": "ITM", "tag": "items", "maxOccurances": -1, "fields": [ {"tag": "code"}, {"tag": "item", "required": true}, {"tag": "quantity", "dataType": "int", "required": true} ] } ] } ``` ``` -------------------------------- ### Convert JSON to EDI String in Ballerina Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/ballerina/README.md Use this snippet to convert a JSON variable representing order data into an EDI string format. Ensure you have the EDI schema loaded correctly. ```ballerina import ballerina/io; import ballerina/edi; public function main() returns error? { // Step 1: Create a JSON variable representing an order json order2 = { "header": { "code": "HDR", "orderId": "ORDER_1201", "organization": "ABC_Store", "date": "2008-01-01" }, "items": [ { "code": "ITM", "item": "A-250", "quantity": 12 }, { "code": "ITM", "item": "B-250", "quantity": 10 } // ... Additional items ... ] }; // Step 2: Load the EDI schema from a JSON file edi:EdiSchema schema = check edi:getSchema(check io:fileReadJson("resources/schema.json")); // Step 3: Convert the JSON order data to EDI string using the schema string orderEDI = check edi:toEdiString(order2, schema); // Step 4: Print the resulting EDI string io:println(orderEDI); } ``` -------------------------------- ### Parse EDI text into JSON using `fromEdiString` Source: https://context7.com/ballerina-platform/module-ballerina-edi/llms.txt Use `fromEdiString` to convert EDI formatted text into a JSON value based on an `EdiSchema`. It handles segment matching, field extraction, and represents unlimited segments as JSON arrays. Returns an `edi:Error` for schema violations. ```ballerina import ballerina/io; import ballerina/edi; public function main() returns error? { edi:EdiSchema schema = check edi:getSchema(check io:fileReadJson("resources/schema.json")); // EDI text: one header segment + five item segments string ediText = check io:fileReadString("resources/sample.edi"); // Content of sample.edi: // HDR*ORDER_1201*ABC_Store*2008-01-01~ // ITM*A-250*12~ // ITM*A-45*100~ // ITM*D-10*58~ // ITM*K-80*250~ // ITM*T-46*28~ json orderData = check edi:fromEdiString(ediText, schema); io:println(orderData.toJsonString()); // Output: // { // "header": {"code":"HDR","orderId":"ORDER_1201","organization":"ABC_Store","date":"2008-01-01"}, // "items": [ // {"code":"ITM","item":"A-250","quantity":12}, // {"code":"ITM","item":"A-45","quantity":100}, // {"code":"ITM","item":"D-10","quantity":58}, // {"code":"ITM","item":"K-80","quantity":250}, // {"code":"ITM","item":"T-46","quantity":28} // ] // } // Access specific fields json header = check orderData.header; string orderId = check header.orderId; io:println(orderId); // Output: ORDER_1201 } ``` -------------------------------- ### Fixed-Length EDI Schema Definition Source: https://context7.com/ballerina-platform/module-ballerina-edi/llms.txt Defines a fixed-length EDI schema using JSON. This schema uses `startIndex` and `length` for field positioning within segments and supports segment reuse via `segmentDefinitions`. It specifies line breaks as segment delimiters and a sentinel value for field delimiters. ```json { "name": "ShipmentStatus", "delimiters": {"segment": "\n", "field": "FL", "component": ":", "repetition": "^"}, "segments": [ {"ref": "HDR", "minOccurances": 1}, { "tag": "Details", "minOccurances": 1, "maxOccurances": -1, "segments": [ {"ref": "S1", "minOccurances": 1}, {"ref": "S2", "maxOccurances": 3} ] } ], "segmentDefinitions": { "HDR": { "code": "1", "tag": "Shipment_Header", "fields": [ {"tag": "Record_Id", "startIndex": 1, "length": 1}, {"tag": "SCAC_code", "startIndex": 4, "length": 4}, {"tag": "Carrier_number", "startIndex": 8, "length": 15}, {"tag": "Total_weight", "startIndex": 80, "length": 6}, {"tag": "Total_quantity", "startIndex": 95, "length": 6, "dataType": "int"} ] }, "S1": { "code": "2", "tag": "Company_Record", "fields": [ {"tag": "Record_Id", "startIndex": 1, "length": 1}, {"tag": "Company_Name", "startIndex": 6, "length": 30}, {"tag": "City", "startIndex": 66, "length": 18}, {"tag": "Zip", "startIndex": 86, "length": 9} ] }, "S2": { "code": "3", "tag": "Status_Record", "fields": [ {"tag": "Status_Code", "startIndex": 4, "length": 2}, {"tag": "Status_Date", "startIndex": 6, "length": 8, "dataType": "int"}, {"tag": "Status_City", "startIndex": 20, "length": 18}, {"tag": "Tractor_ID", "startIndex": 40, "length": 13} ] } } } ``` -------------------------------- ### Define SimpleOrder EDI Schema Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/ballerina/README.md Defines the structure for a simple order, including segments, fields, and data types. This JSON schema is used to parse EDI documents. ```json { "name": "SimpleOrder", "delimiters" : {"segment" : "~", "field" : "*", "component": ":", "repetition": "^"}, "segments" : [ { "code": "HDR", "tag" : "header", "minOccurances": 1, "fields" : [{"tag": "code"}, {"tag" : "orderId"}, {"tag" : "organization"}, {"tag" : "date"}] }, { "code": "ITM", "tag" : "items", "maxOccurances" : -1, "fields" : [{"tag": "code"}, {"tag" : "item"}, {"tag" : "quantity", "dataType" : "int"}] } ] } ``` -------------------------------- ### Delimited EDI Schema Definition Source: https://context7.com/ballerina-platform/module-ballerina-edi/llms.txt Defines a delimited EDI schema using JSON. This schema specifies segment and field delimiters, and structures for segments, fields, and components. It supports options to control parsing behavior like ignoring segments or preserving empty fields. ```json { "name": "InvoiceSchema", "delimiters": {"segment": "~", "field": "*", "component": ":", "repetition": "^"}, "ignoreSegments": ["UNA", "UNZ"], "preserveEmptyFields": false, "includeSegmentCode": true, "segments": [ { "code": "INV", "tag": "invoice", "minOccurances": 1, "maxOccurances": 1, "fields": [ {"tag": "code"}, {"tag": "invoiceNumber", "required": true}, {"tag": "totalAmount", "dataType": "float"}, { "tag": "billingAddress", "dataType": "composite", "components": [ {"tag": "street", "dataType": "string"}, {"tag": "city", "dataType": "string"}, {"tag": "zip", "dataType": "string"} ] } ] }, { "code": "LIN", "tag": "lineItems", "maxOccurances": -1, "fields": [ {"tag": "code"}, {"tag": "productId", "required": true}, {"tag": "quantity", "dataType": "int", "required": true}, {"tag": "unitPrice", "dataType": "float", "required": true} ] } ] } ``` -------------------------------- ### Convert JSON to EDI Text Source: https://github.com/ballerina-platform/module-ballerina-edi/blob/main/docs/specs/ModuleSpecification.md Serializes a JSON object into an EDI text string according to a specified `EdiSchema`. This function is useful for generating EDI files from structured data. It requires the `EdiSchema` to be previously defined. ```ballerina function toEdiString(json msg, EdiSchema schema) returns string|Error ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.