Dynamics 365 Business Central – Create a JSON File with AL

JSON (JavaScript Object Notation) is an open standard file format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other serializable values).

In today’s digital age, the exchange of data between Microsoft Dynamics 365 Business Central and other systems is often necessary. In a previous post, I covered a basic example of creating an XML file with AL. Along with XML files, I am often asked how to generate JSON files. Here is an example of creating a basic JSON file in AL.

The  example below demonstrates a basic model to provide enough information to create a simple file; consider it “Getting Started.” There are many ways to code a solution, and opinion or situation determines the best. Within Microsoft Dynamics 365 Business Central, JSON can be processed codeunit 5459 “JSON Management.” I  do use this codeunit often and may cover this example using the codeunit in the future.


    local procedure CreateJSON(Customer: Record Customer)
    var
        ShiptoAddress: Record "Ship-to Address";
        JArray: JsonArray;
        CustObject: JsonObject;
        JsonObject: JsonObject;
        ShiptoObject: JsonObject;
        JsonData: Text;

    begin
        Clear(JsonObject);
        Clear(CustObject);

        CustObject.Add('No', Customer."No.");
        CustObject.Add('Address', Customer.Address);
        CustObject.Add('Address_2', Customer."Address 2");
        CustObject.Add('City', Customer.City);
        CustObject.Add('County', Customer.County);
        CustObject.Add('Country_Region', Customer."Country/Region Code");
        CustObject.Add('Post_Code', Customer."Post Code");

        ShiptoAddress.SetRange("Customer No.", Customer."No.");
        if ShiptoAddress.FindSet() then begin
            Clear(JArray);
            repeat
                Clear(ShiptoObject);
                ShiptoObject.Add('Code', ShiptoAddress.Code);
                ShiptoObject.Add('Address', ShiptoAddress.Address);
                ShiptoObject.Add('Address_2', ShiptoAddress."Address 2");
                ShiptoObject.Add('City', ShiptoAddress.City);
                ShiptoObject.Add('County', ShiptoAddress.County);
                ShiptoObject.Add('Post_Code', ShiptoAddress."Post Code");
                JArray.Add(ShiptoObject);
            until ShiptoAddress.Next() = 0;
            CustObject.Add('Ship-to', JArray);
        end;

        JsonObject.Add('Customer', CustObject);

        JsonObject.WriteTo(JsonData);

    end;

The above code creates the 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"
            }
        ]
    }
}

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.

4 comments

3 pings

Skip to comment form

    • Rayan on January 31, 2023 at 11:56 pm
    • Reply

    Hi,
    How to write this json,
    [
    {
    “transaction”: {
    “Version”: “1.1”,
    “TranDtls”: {
    “TaxSch”: “GST”,
    “SupTyp”: “B2B”,
    “RegRev”: “Y”,
    “EcmGstin”: “”,
    “IgstOnIntra”: “N”
    }
    }
    }
    ]
    I need your help
    Regards
    Rayan

  1. Based on your example, a literal translation, using the model discussed in the post would be as listed below. This can be adapted to support your needs.

    local procedure CreateJSON2(): Text;
    var
    JArray: JsonArray;
    JsonObject: JsonObject;
    TranDtlsObject: JsonObject;
    TransactionObject: JsonObject;
    JsonData: Text;

    begin
    Clear(JArray);
    Clear(JsonObject);
    Clear(TranDtlsObject);
    Clear(TransactionObject);

    TranDtlsObject.Add(‘TaxSch’, ‘GST’);
    TranDtlsObject.Add(‘SupTyp’, ‘B2B’);
    TranDtlsObject.Add(‘RegRev’, ‘Y’);
    TranDtlsObject.Add(‘EcmGstin’, ”);
    TranDtlsObject.Add(‘IgstOnIntra’, ‘N’);

    TransactionObject.Add(‘Version’, ‘1.1’);
    TransactionObject.Add(‘TranDtls’, TranDtlsObject);

    JsonObject.Add(‘transaction’, TransactionObject);
    JArray.Add(JsonObject);

    JArray.WriteTo(JsonData);

    exit(JsonData);

    end;

    • Noman Ejaz on September 20, 2023 at 4:48 am
    • Reply

    How to access this codeunit API in postman.

    • RICHARD on September 29, 2023 at 11:09 am
    • Reply

    hola como estan,
    como obtendria el valor de la estructura del json. necesito obtener por ejemplo el “Address”
    “Ship-to”: [
    {
    “Code”: “LEWES ROAD”,
    “Address”: “2 Lewes Road”,
    “Address_2”: “”,
    }

  1. […] 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 […]

  2. […] 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 […]

  3. […] Dynamics 365 Business Central – Create a JSON File with AL – DvlprLife.com […]

Leave a Reply to Dvlpr Cancel reply

Your email address will not be published.