Dynamics 365 Business Central – Read a JSON File with AL

A JSON (JavaScript Object Notation) file is a standard text file used to store data in a structured, organized way. It is a simple, human-readable format often used for transmitting data between a server and a web application or between different systems.

A JSON file consists of a series of key-value pairs separated by commas. The keys are always strings, and the values can be strings, numbers, booleans, arrays, or objects.

I had previously discussed an example demonstrating a basic model with enough information to create a simple JSON file with AL for Microsoft Dynamics 365 Business Central. The example created a JSON file displayed here:

    "Customer": {
        "No": "10000",
        "Address": "192 Market Square",
        "Address_2": "",
        "City": "",
        "County": "NJ",
        "Country_Region": "US",
        "Post_Code": "",
        "Ship-to": [
                "Code": "LEWES ROAD",
                "Address": "2 Lewes Road",
                "Address_2": "",
                "City": "Atlanta",
                "County": "GA",
                "Post_Code": "31772"
                "Code": "PARK ROAD",
                "Address": "10 Park Road",
                "Address_2": "",
                "City": "Atlanta",
                "County": "GA",
                "Post_Code": "31772"

The example below shows a basic model to provide enough information to get started reading a simple JSON, resembling the file above, with AL for Microsoft Dynamics 365 Business Central using Codeunit 5459 “JSON Management.”

There are many ways to code a solution, and opinion or situation determines the best (including the Json datatypes). Codeunit 5459 “JSON Management” has many methods for working with JSON files. I encourage you to explore the codeunit.

local procedure ReadJSON(JsonObjectText: Text)
        Customer: Record Customer;
        ShiptoAddress: Record "Ship-to Address";
        ArrayJSONManagement: Codeunit "JSON Management";
        JSONManagement: Codeunit "JSON Management";
        ObjectJSONManagement: Codeunit "JSON Management";
        i: Integer;
        CodeText: Text;
        CustomerJsonObject: Text;
        JsonArrayText: Text;
        ShipToJsonObject: Text;
        if JSONManagement.GetArrayPropertyValueAsStringByName('Customer', CustomerJsonObject) then begin

            ObjectJSONManagement.GetStringPropertyValueByName('No', CodeText);
            Customer.Validate("No.", CopyStr(CodeText.ToUpper(), 1, MaxStrLen(Customer."No.")));
            ObjectJSONManagement.GetStringPropertyValueByName('Address', CodeText);
            Customer.Validate("Address", CopyStr(CodeText, 1, MaxStrLen(Customer."Address")));

            if JSONManagement.GetArrayPropertyValueAsStringByName('Ship-to', JsonArrayText) then begin
                for i := 0 to ArrayJSONManagement.GetCollectionCount() - 1 do begin
                    ArrayJSONManagement.GetObjectFromCollectionByIndex(ShipToJsonObject, i);

                    ShiptoAddress.Validate("Customer No.", Customer."No.");
                    ObjectJSONManagement.GetStringPropertyValueByName('Code', CodeText);
                    ShiptoAddress.Validate("Code", CopyStr(CodeText.ToUpper(), 1, MaxStrLen(ShiptoAddress.Code)));
                    ShiptoAddress.Validate("Address", CopyStr(CodeText, 1, MaxStrLen(ShiptoAddress.Address)));


Note: The code and information discussed in this article are for informational and demonstration purposes only. This content was created referencing Microsoft Dynamics 365 Business Central 2022 Wave 2 online.

Leave a Reply

Your email address will not be published.