SBX - Search With Button

SBX - Forum Post Title

Creating JSON data in AL JsonObjectClass

Dynamics 365 Business Central Forum

Seema ANM asked a question on 29 Jul 2018 7:17 PM

Question Status

Suggested Answer

Hi

Sample code needed to create JSON data using JsonObjectClass in AL using Visual studio code

 

Thanks

Seema

Reply
Nissay Halas Tia responded on 29 Jul 2018 9:01 PM
My Badges

Hi Seema,

1) have a look on this article (you can use it as a white paper for how to work with JSON with AL)

   https://www.kauffmann.nl/2017/06/24/al-support-for-rest-web-services/

2) There is a nice extension for Visual Studio Code by Tobias Fenster that auto-generate AL code based on a JSON file: 

   https://www.axians-infoma.de/navblog/generating-al-objects-from-json/

   Note: The Tobias's extension is based on the first article above.

.

Please verify my answer if you find my answer helpful. Doing so you'll show other community members that there was found a solution and you credit my help.

,

Best regards

Reply
Seema ANM responded on 30 Jul 2018 12:30 AM

Hi Nissay,

Thanks for link , the Article is good for reading JSON data with AL . I want code creating JSON data in AL using JSONOBJECT, ARRAY etc  

  www.kauffmann.nl/.../al-support-for-rest-web-services

Thanks

Seema ANM

Reply
Nissay Halas Tia responded on 30 Jul 2018 12:58 PM
My Badges
Suggested Answer

Hi again Seema,

Sorry I misunderstood your need.

Well this is an example on how to create a JSON with AL for a Sales Order (Sales Header and Sales Line).

1) This is the resulted JSON file/the JSON to create:

2) Here is the code in AL:

    procedure CreateJsonProcedure()
    var
        VJsonObjectHeader: JsonObject;
        VJsonObjectLines: JsonObject;
        VJsonArray: JsonArray;
        VJsonArrayLines: JsonArray;
        VSalesHeader: Record "Sales Header";
        VSalesLines: Record "Sales Line";
    begin

        // Header
        VSalesHeader.Reset();
        VSalesHeader.SetFilter("Document Type", '%1', VSalesHeader."Document Type"::Order);
        VSalesHeader.FindLast();

        VJsonObjectHeader.Add('Sales Order No.', VSalesHeader."No.");
        VJsonObjectHeader.Add('Bill-to Name', VSalesHeader."Bill-to Name");
        VJsonObjectHeader.Add('Order Date', VSalesHeader."Order Date");

        VJsonArray.Add(VJsonObjectHeader);

        //Lines
        VSalesLines.Reset();
        VSalesLines.SetFilter("Document Type", '%1', VSalesLines."Document Type"::Order);
        VSalesLines.SetFilter("Document No.", VSalesHeader."No.");
        VSalesLines.FindSet();

        // JsonObject Init
        VJsonObjectLines.Add('Line No.', '');
        VJsonObjectLines.Add('Item No.', '');
        VJsonObjectLines.Add('Location Code', '');
        VJsonObjectLines.Add('Quantity', '');

        repeat

            VJsonObjectLines.Replace('Line No.', VSalesLines."Line No.");
            VJsonObjectLines.Replace('Item No.', VSalesLines."No."); 
            VJsonObjectLines.Replace('Location Code', VSalesLines."Location Code");
            VJsonObjectLines.Replace('Quantity', VSalesLines.Quantity);

            VJsonArrayLines.Add(VJsonObjectLines);

        until VSalesLines.Next() = 0;

        VJsonArray.Add(VJsonArrayLines);

    
    end;


You can use the Message function to view the structure of your JSON :

        VJsonText: Text;  // in Var section
VJsonArray.WriteTo(VJsonText); Message('My JSON : %1', VJsonText);

.

Please verify my answer if you find my answer helpful. Doing so you'll show other community members that there was found a solution and you credit my help.

,

Best regards

Reply
chou chihming responded on 22 Feb 2019 1:45 AM
VJsonObjectLines.Replace('Line No.', VSalesLines."Line No.");

[

{
"Line No.": 20000,
"Item No.": "1964-W",
"Location Code": "GREEN",
"Quantity": 2.0
},
{
"Line No.": 20000,
"Item No.": "1964-W",
"Location Code": "GREEN",
"Quantity": 2.0
}
]
Reply
Fommo responded on 14 Jun 2019 11:17 AM

As the Json data types are references you need to use the Clone function when adding to the array:

 VJsonArrayLines.Add(VJsonObjectLines.Clone());

This will create a deep copy of the JsonObject and add it to the array. This way you can use Replace on the VJsonObjectLines without replacing the data that is already added to the JsonArray.

Reply
Nissay Halas Tia responded on 30 Jul 2018 12:58 PM
My Badges
Suggested Answer

Hi again Seema,

Sorry I misunderstood your need.

Well this is an example on how to create a JSON with AL for a Sales Order (Sales Header and Sales Line).

1) This is the resulted JSON file/the JSON to create:

2) Here is the code in AL:

    procedure CreateJsonProcedure()
    var
        VJsonObjectHeader: JsonObject;
        VJsonObjectLines: JsonObject;
        VJsonArray: JsonArray;
        VJsonArrayLines: JsonArray;
        VSalesHeader: Record "Sales Header";
        VSalesLines: Record "Sales Line";
    begin

        // Header
        VSalesHeader.Reset();
        VSalesHeader.SetFilter("Document Type", '%1', VSalesHeader."Document Type"::Order);
        VSalesHeader.FindLast();

        VJsonObjectHeader.Add('Sales Order No.', VSalesHeader."No.");
        VJsonObjectHeader.Add('Bill-to Name', VSalesHeader."Bill-to Name");
        VJsonObjectHeader.Add('Order Date', VSalesHeader."Order Date");

        VJsonArray.Add(VJsonObjectHeader);

        //Lines
        VSalesLines.Reset();
        VSalesLines.SetFilter("Document Type", '%1', VSalesLines."Document Type"::Order);
        VSalesLines.SetFilter("Document No.", VSalesHeader."No.");
        VSalesLines.FindSet();

        // JsonObject Init
        VJsonObjectLines.Add('Line No.', '');
        VJsonObjectLines.Add('Item No.', '');
        VJsonObjectLines.Add('Location Code', '');
        VJsonObjectLines.Add('Quantity', '');

        repeat

            VJsonObjectLines.Replace('Line No.', VSalesLines."Line No.");
            VJsonObjectLines.Replace('Item No.', VSalesLines."No."); 
            VJsonObjectLines.Replace('Location Code', VSalesLines."Location Code");
            VJsonObjectLines.Replace('Quantity', VSalesLines.Quantity);

            VJsonArrayLines.Add(VJsonObjectLines);

        until VSalesLines.Next() = 0;

        VJsonArray.Add(VJsonArrayLines);

    
    end;


You can use the Message function to view the structure of your JSON :

        VJsonText: Text;  // in Var section
VJsonArray.WriteTo(VJsonText); Message('My JSON : %1', VJsonText);

.

Please verify my answer if you find my answer helpful. Doing so you'll show other community members that there was found a solution and you credit my help.

,

Best regards

Reply

SBX - Two Col Forum

SBX - Migrated JS