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
Post a Comment