Handling XML and JSON files with the AL language

The AL language extension has native support for handling XML and JSON documents.

An XML document is represented by using the XmlDocument data type, as explained at https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/xmldocument/xmldocument-data-type.

The following code shows how you can import an XML file and load it into an XmlDocument object:

local procedure ImportXML()
var
TempBlob : Codeunit "Temp Blob";
TargetXmlDoc : XmlDocument;
XmlDec : XmlDeclaration;
Instr: InStream;
filename: Text;
begin
// Create the Xml Document
TargetXmlDoc := XmlDocument.Create;
xmlDec := xmlDeclaration.Create('1.0','UTF-8','');
TargetXmlDoc.SetDeclaration(xmlDec);

// Create an Instream object & upload the XML file into it
TempBlob.CreateInStream(Instr);
filename := 'data.xml';
UploadIntoStream('Import XML','','',filename,Instr);

// Read stream into new xml document
Xmldocument.ReadFrom(Instr, TargetXmlDoc);
end;

Here, we have created an XmlDocument object with an XML declaration, then we have created an InStream object to load the XML file, and we have read the InStream content into the XmlDocument object.

If you reference the TargetXmlDoc object, you see all of the available methods for handling and manipulating the XML file:

To create an XML document directly from AL code, you can use the XmlDocument and XmlElement classes:

local procedure XMLDocumentCreation()
var
xmldoc: XmlDocument;
xmlDec: XmlDeclaration;
node1: XmlElement;
node2: XmlElement;
begin
xmldoc := XmlDocument.Create();
xmlDec := xmlDeclaration.Create('1.0','UTF-8','');
xmlDoc.SetDeclaration(xmlDec);
node1:= XmlElement.Create('node1');
xmldoc.Add(node1);
node2 := XmlElement.Create('node2');
node2.SetAttribute('ID','3');
node1.Add(node2);
end;

This code creates an XML document with a root node (called node1) and a child node (called node2) with an ID attribute that has a value of 3 (<node2 ID="3">).

Native support for JSON documents is provided by using the JsonObject and JsonArray data types. Each of these data types contains the methods for handling a JSON file (both reading and writing) and for manipulating the JSON data (tokens).

The following code shows an example of how to create a JSON representation of a sales order document:

procedure CreateJsonOrder(OrderNo: Code[20])
var
JsonObjectHeader: JsonObject;
JsonObjectLines: JsonObject;
JsonOrderArray: JsonArray;
JsonArrayLines: JsonArray;
SalesHeader: Record "Sales Header";
SalesLines: Record "Sales Line";

begin
//Retrieves the Sales Header
SalesHeader.Get(SalesHeader."Document Type"::Order,OrderNo);
//Creates the JSON header details
JsonObjectHeader.Add('sales_order_no', SalesHeader."No.");
JsonObjectHeader.Add(' bill_to_customer_no', SalesHeader."Bill-to Customer No.");
JsonObjectHeader.Add('bill_to_name', SalesHeader."Bill-to Name");
JsonObjectHeader.Add('order_date', SalesHeader."Order Date");
JsonOrderArray.Add(JsonObjectHeader);

//Retrieves the Sales Lines
SalesLines.SetRange("Document Type", SalesLines."Document Type"::Order);
SalesLines.SetRange("Document No.", SalesHeader."No.");
if SalesLines.FindSet then
// JsonObject Init
JsonObjectLines.Add('line_no', '');
JsonObjectLines.Add('item_no', '');
JsonObjectLines.Add('description', '');
JsonObjectLines.Add('location_code', '');
JsonObjectLines.Add('quantity', '');
repeat
JsonObjectLines.Replace('line_no', SalesLines."Line No.");
JsonObjectLines.Replace('item_no', SalesLines."No.");
JsonObjectLines.Replace('description', SalesLines.Description);
JsonObjectLines.Replace('location_code', SalesLines."Location Code");
JsonObjectLines.Replace('quantity', SalesLines.Quantity);
JsonArrayLines.Add(JsonObjectLines);
until SalesLines.Next() = 0;
JsonOrderArray.Add(JsonArrayLines);
end;

This procedure receives an order number as input, retrieves the Sales Header and Sales Line details, and creates a JSON representation. The final result is as follows:

[
{
"sales_order_no": "SO1900027",
"bill_to_customer_no": "C001435",
"bill_to_name": "Packt Publishing",
"order_date": "2019-03-23"
},
[
{
"line_no": "10000",
"item_no": "IT00256",
"description": "Dynamics 365 Business Central Development Guide",
"location_code": "MAIN",
"quantity": 30
},
{
"line_no": "20000",
"item_no": "IT03465",
"description": "Mastering Dynamics 365 Business Central",
"location_code":"MAIN",
"quantity": 27
}
]
]

Obviously, you can also receive a JSON representation as input (for example, as a response from an API call) and handle it using the same data types.

Here, we have seen how to handle JSON documents in AL by using the native JSON types.

In the next section, we'll see a complete example on how to call an API, receive a JSON response, parse it, and save data to a Dynamics 365 Business Central entity.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.108.9