Hi
Sample code needed to create JSON data using JsonObjectClass in AL using Visual studio code
Thanks
Seema
Hi
Sample code needed to create JSON data using JsonObjectClass in AL using Visual studio code
Thanks
Seema
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.
[
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
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
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
André Arnaud de Cal... 291,387 Super User 2024 Season 2
Martin Dráb 230,432 Most Valuable Professional
nmaenpaa 101,156