How to Create JSON file in AL for D365 BC

Create JSON file in AL for D365 BC:

(1) Introduction: A JSON file is a file that stores data in a structured way. It stands for JavaScript Object Notation. It is mainly used for transferring data from web applications to servers.

(2) Example of JSON File Structure as shown below.

“password”: “abc@123”,

(3) JSON structure is a combination of:

  • Data is in value pairs: ‘qwer
  • Data is separated by commas: ‘,
  • Curly braces hold objects: ‘{}
  • Square brackets hold arrays: ‘[]

(4) In this blog we will see the steps of creating a JSON file in AL for D365 BC. If you develop a JSON file in the previous version then you will find major differences while developing a JSON file in AL.

(5) Let’s create Sales Invoice detail in JSON file format. For creating the JSON file, I am creating a new Function in the Codeunit object.

(6) For creating JSON Files in D365 BC, Microsoft introduces datatypes (JsonObject and JsonArray).

(7) In the previous version these datatypes were not available and the method of creating JSON files was also different.

(8) Follow the below images to create JSON file format.

(9) Steps for getting information of Sales Invoice in JSON format, as shown.

Define JsonObject and JsonArray in D365 BC
Group into document_details in bc d365
group into seller and buyer details in business central
Group into Item_List with array in Business Central

(10) According to the above images, I have declared JsonObjects like “JObjectDocumentDetails”, “JObjectSellerDetails”, “ JObjectBuyerDetails” and “JobjectItemList” and then combined these JsonObject in one JsonObject. If your JSON file format is not grouped then you declare only one JSON object. For more clarity refer to the below example of JSON files.

  • Plain JSON:
“password”: “abc@123”,
  • Grouped JSON:
“password”: “abc@123”,

Source Code:

codeunit 50001 “Custom Functions-01”
    procedure CreateJSONSalesInvoice(SalesInvoiceHeader: Record “Sales Invoice Header”)
GetLocation: Record Location;
GetCustomer: Record Customer;
JObjectDocumentDetails: JsonObject;
JObjectSellerDetails: JsonObject;
JObjectBuyerDetails: JsonObject;
JobjectItemList: JsonObject;
        SalesInvoiceLine3: Record “Sales Invoice Line”;
ShiptoAddress: Record “Ship-to Address”;
CustShipGstin: Code[15];
RefDocNo: Code[20];
        SalesInvoiceHeader2: Record “Sales Invoice Header”;
        SalesInvoiceLine2: Record “Sales Invoice Line”;
InvRoundingGL: Code[20];
SrNo: Integer;
SalesInvoiceLine: Record “Sales Invoice Line”;
DocumentNo: Code[20];
JObject: JsonObject;
JsonText: Text;
JArray: JsonArray;
        J: J
CompanyInformation: record “Company Information”;
DocumentNo := SalesInvoiceHeader.”No.”;
GetLocation.GET(SalesInvoiceHeader.”Location Code”);
GetLocation.TestField(“GST Registration No.”);
GetCustomer.GET(SalesInvoiceHeader.”Bill-to Customer No.”);
        //Json Sales Invoice document detail
JObjectDocumentDetails.Add(‘document_number’, SalesInvoiceHeader.”No.”);
JObjectDocumentDetails.Add(‘document_date’, SalesInvoiceHeader.”Posting Date”);
JObject.Add(‘document_details’, JObjectDocumentDetails);
        //Json Sales Invoice document detail
        //Json Sales Invoice Seller detail
JObjectSellerDetails.Add(‘legal_name’, CompanyInformation.Name);
JObjectSellerDetails.Add(‘trade_name’, GetLocation.Name);
JObjectSellerDetails.Add(‘address1’, GetLocation.Address);
JObjectSellerDetails.Add(‘address2’, GetLocation.”Address 2″);
JObjectSellerDetails.Add(‘location’, GetLocation.City);
JObjectSellerDetails.Add(‘pincode’, GetLocation.”Post Code”);
JObject.Add(‘seller_details’, JObjectSellerDetails);
        //Json Sales Invoice Seller detail
        //Json Sales Invoice Buyer detail
JObjectBuyerDetails.Add(‘legal_name’, SalesInvoiceHeader.”Bill-to Name”);
JObjectBuyerDetails.Add(‘trade_name’, SalesInvoiceHeader.”Bill-to Name”);
JObjectBuyerDetails.Add(‘address1’, SalesInvoiceHeader.”Bill-to Address”);
JObjectBuyerDetails.Add(‘address2’, SalesInvoiceHeader.”Bill-to Address 2″);
JObjectBuyerDetails.Add(‘location’, SalesInvoiceHeader.”Bill-to City”);
JObject.Add(‘buyer_details’, JObjectBuyerDetails);
        //Json Sales Invoice Buyer detail
        //Json Sales Invoice Item List
SalesInvoiceLine.SETRANGE(“Document No.”, SalesInvoiceHeader.”No.”);
SalesInvoiceLine.SETFILTER(“No.”, ‘<>%1’, InvRoundingGL);
SalesInvoiceLine.SETFILTER(Quantity, ‘<>%1’, 0);
        IF SalesInvoiceLine.FINDSET THEN BEGIN
SrNo += 1;
JobjectItemList.Add(‘item_serial_number’, SrNo);
JobjectItemList.Add(‘product_description’, SalesInvoiceLine.Description);
JobjectItemList.Add(‘quantity’, SalesInvoiceLine.Quantity);
JobjectItemList.Add(‘unit’, SalesInvoiceLine.”Unit of Measure Code”);
JobjectItemList.Add(‘unit_price’, ROUND(SalesInvoiceLine.”Unit Price” * 1, 0.01, ‘=’));
JobjectItemList.Add(‘total_amount’, ROUND((SalesInvoiceLine.”Unit Price” * SalesInvoiceLine.Quantity) * 1, 0.01, ‘=’));
            until SalesInvoiceLine.Next = 0;
JObject.Add(‘item_list’, JArray);
        //Json Item list

Automatic Posting of Sales Shipment at the time of Sales Invoice Posting
Sales Invoice Report Format and Development in D365 BC
Sales Shipped not Invoiced Report in BC D365
Sales Register Report in D365 BC Format and Development
Record.TransferFields(var Record [, Boolean]) Method– Microsoft Docs

Leave a Reply