Skip to main content

How to Create & Download the Json in Business Central : Using Json Datatypes

Hi Experts,

In this article i am learning & sharing with you guys that how to Create Json structure in Business Central.
In this example, i am creating Json structure from Customer table and store that Json file in local system.
Codeunit 50101 "Json Write"
{
    trigger OnRun()
    var
        Customer_Rec: Record Customer;
        JObject_Cust: JsonObject;
        JObject_Address: JsonObject;
        JArray_Address: JsonArray;
        JObject_PostingGroup: JsonObject;
        JArray_PostingGroup: JsonArray;
        RecTempBlob: Record TempBlob;
        InStr: InStream;
        OutStr: OutStream;
        FileName: Text;
        Result: Text;

    begin
        // Code to Create Json file Structure_Start
        Customer_Rec.get('10000');
        JObject_Cust.Add('No.', Customer_Rec."No.");
        JObject_Cust.Add('Name', Customer_Rec.Name);
        JObject_Address.Add('Address', Customer_Rec.Address);
        JObject_Address.Add('City', Customer_Rec.City);
        JObject_Address.Add('Country', Customer_Rec."Country/Region Code");
        JArray_Address.Add(JObject_Address);
        JObject_Cust.Add('Address Details', JArray_Address);
        JObject_PostingGroup.Add('GBPG', Customer_Rec."Gen. Bus. Posting Group");
        JObject_PostingGroup.Add('CPG', Customer_Rec."Customer Posting Group");
        JArray_PostingGroup.Add(JObject_PostingGroup);
        JObject_Cust.Add('Posting Groups', JArray_PostingGroup);
        // Code to Create Json file Structure_End

        //Code to download the Json File_Start
        FileName := 'Customer.Json';
        RecTempBlob.Blob.CreateInStream(InStr);
        RecTempBlob.Blob.CreateOutStream(OutStr);
        JObject_Cust.WriteTo(OutStr);
        OutStr.WriteText(Result);
        InStr.ReadText(Result);
        DownloadFromStream(InStr, 'Download Json File', '', '', FileName);
        //Code to download the Json File_End
    end;
}

Action button is created on Customer List Page "Create Json" to run the above Codeunit.

pageextension 50106 "Customer List Ext" extends "Customer List"
{
    layout
    {
        // Add changes to page layout here
    }

    actions
    {
        // Add changes to page actions here
        addlast(Processing)
        {
            action("Create Json")
            {
                ApplicationArea = All;
                Image = ExportFile;

                trigger OnAction()
                Var
                    JsonWrite_CU: Codeunit "Json Write";
                    ConfirmMsg: Label 'Do you want to download the Json File?';
                    ElseMsg: Label 'Process Cancelled.';
                begin
                    if Confirm(ConfirmMsg, false) then
                        JsonWrite_CU.Run()
                    else
                        Message(ElseMsg);
                end;
            }
        }
    }
}

Output:- After selecting the path and align the Json data, it looks like:


Thanks for Reading!!
#Follow #KunalBlog #Microsoft #BusinessCentral

Comments

Popular posts from this blog

Amount In Words in Navision / Business Central

Hi Guys, In this article, we will see how to display amount in words in Microsoft Dynamics Business Central. Here, I am taking an example of Report object  which is widely used in Navision for converting decimal amount field into words. Procedure :-  Step-1 Declare the following Global Variables :         RepCheck:  Report  "Check";         NoText:  array [ 2 ]  of   Text ;         AmountInWords:  Text ; where, (1) RepCheck is a variable of  Report DataType Named as " Check " which is 1401. (2) NoText is a variable of DataType " Text " with array Dimension value as 2 which is defined as shown above. (3) AmountInWords is a variable of DataType " Text " which will store final result of amount in words. Step-2 Now, add the following code in OnAfterGetRecord() trigger of DataItem containing that decimal field value. So in my custom Purchase Order Report, i am adding code in the Purchase Header (DataItem) - OnAfterGetRecord() trigger.   trigger  OnA

Flow Custom field to G/L Entry and other Posted tables : From Purchase Order to Posted Purchase invoice

Hi Readers, In this blog we will see how we can transfer data from " Purchase Header" to "Purchase Invoice Header" & "G/L Entry" table in Business Central. In this example, will create "Remarks" field on Purchase Header table and after posting, flow it to Purchase Invoice Header & G/L Entry tables: Firsly, create required fields ("Remarks") in below mentioned tables by creating table extensions: Purchase Header (38) Purch. Inv. Header (122) Gen. Journal Line (81)  G/L Entry (17) For the Purch. Inv. Header table, you can just create copy the field from purchase header and paste it in  Purch. Inv. Header  table. If the field ID No. is same for that field for both the tables then you don't have to code i.e, it automatically transfer that field data from Purch. Header to Purch. Inv. Header . Step-(1)   tableextension   50104  "Purchase Header Ext"  extends  "Purchase Header" {      fields     {         

Calculate Date : Using Date Formula and CalcDate Function in Navision/BC

 Hi Everyone, In this article we will see how we can Calculate Date by adding and subtracting No. of Days / Months based on CalcDate and DateFormula datatype. Here, I define three variables required for: (1) " No of Days " -  To define No. of Days / Months as required. You can also use these units denoted as: <Unit> = D | WD | W | M | Q | Y (D=day, WD=weekday, W=week, M=month, Q=quarter, Y=year). (2) " From Date " and " To Date "  Date type Variables to store calculated date in it. page   50103  "Date from Date Formula" {     PageType = Card;     ApplicationArea = All;     UsageCategory = Administration;     SourceTable =  Integer ;      layout     {          area( Content )         {              group( General )             {                  field( "No. of Days"; "No. of Days" )                 {                     ApplicationArea = All;                 }             }         }     }      actions     {          area