Business Central 2024 wave 2: Yaml-support in JsonObject (YAML can be read into a JsonObject)

Dynamics 365 Business Central

Hi, Readers.
And Business Central 2024 wave 2 CU04 (BC25.4) has been released this month. More details: Cumulative Update Summary for Microsoft Dynamics 365 Business Central (February, 2025)

AL Language extension Changelog Version 14.3 has been updated, in this post, I would like to briefly share one of the updates.

Yaml-support in JsonObject: Support for YAML is add to the JsonObject. YAML can be read into a JsonObject and be manipulated using the JsonObject API.

YAML is a human-friendly data serialization language for all programming languages. It is commonly used for configuration files and in applications where data is being stored or transmitted. More details: YAML and YAML Tutorial: A Complete Language Guide with Examples

And pipelines in Azure DevOps can be configured with YAML files. These text files contain all the different steps the build process has to follow and execute. You can use this in your build pipeline. More details: Work with YAML files

Starting with runtime 14.3, we can read YAML into JsonObject data type and write JsonObject data type into YAML.👏👏👏

PS: If you encounter the following error, please update AL Language to the latest version.

Let’s look at a simple example: Excerpted from Work with YAML files

PS: There are some external tools that also support YAML to JSON conversion, for example, YAML to JSON Converter:

First let’s read all the contents of the “stages“.

Great.

PS: Same as JSON file, the converted YAML data contains arrays, objects and values. This can be confirmed during debugging.

You can also recognize them via json file and yaml file
Json:

  • Array is enclosed by square brackets: []
  • Object is enclosed by curly brackets: {}
  • Values ​​are in the format “key”: “value”

yaml:

  • Each dash “-” represents an array

Then let’s look at two simple examples.
1. Read the value of “appBuild

Test code:

pageextension 50119 CustomerListExt extends "Customer List"
{
    actions
    {
        addafter("Co&mments")
        {
            action(ReadYAML)
            {
                Caption = 'Read YAML';
                ApplicationArea = All;
                Promoted = true;
                PromotedCategory = Process;
                Image = GetLines;

                trigger OnAction()
                var
                    JsonObject: JsonObject;
                    JsonToken: JsonToken;
                    JsonTokenPlatform: JsonToken;
                    JsonObjectVariables: JsonObject;
                    JsonTokenVariables: JsonToken;
                    JsonArrayStages: JsonArray;
                    JsonValue: JsonValue;
                    JsonArrayJobs: JsonArray;
                    JsonTokenLoopJobs: JsonToken;
                    JsonObjectLoopJobs: JsonObject;
                    JsonTokenLoopStages: JsonToken;
                    JsonObjectLoopStages: JsonObject;
                    InS: InStream;
                    FileName: Text;
                    Msg: Text;
                    UploadFileMsg: Label 'Upload a YAML file';
                begin
                    if UploadIntoStream(UploadFileMsg, '', '', FileName, InS) then begin
                        JsonObject.ReadFromYaml(InS);
                        if JsonObject.Get('stages', JsonToken) then
                            JsonArrayStages := JsonToken.AsArray();
                        foreach JsonTokenLoopStages in JsonArrayStages do begin
                            JsonObjectLoopStages := JsonTokenLoopStages.AsObject();
                            if JsonObjectLoopStages.Get('jobs', JsonTokenLoopStages) then
                                JsonArrayJobs := JsonTokenLoopStages.AsArray();
                            foreach JsonTokenLoopJobs in JsonArrayJobs do begin
                                JsonObjectLoopJobs := JsonTokenLoopJobs.AsObject();
                                if JsonObjectLoopJobs.Get('variables', JsonTokenVariables) then
                                    JsonObjectVariables := JsonTokenVariables.AsObject();
                                if JsonObjectVariables.Get('appBuild', JsonTokenPlatform) then begin
                                    JsonValue := JsonTokenPlatform.AsValue();
                                    Msg := JsonValue.AsText();
                                    Message(Msg);
                                end;
                            end;
                        end;
                    end;
                end;
            }
        }
    }
}

2. Read the three “displayName” in steps.

Test code:

pageextension 50119 CustomerListExt extends "Customer List"
{
    actions
    {
        addafter("Co&mments")
        {
            action(ReadYAML)
            {
                Caption = 'Read YAML';
                ApplicationArea = All;
                Promoted = true;
                PromotedCategory = Process;
                Image = GetLines;

                trigger OnAction()
                var
                    JsonObject: JsonObject;
                    JsonToken: JsonToken;
                    JsonTokenDisplayName: JsonToken;
                    JsonTokenSteps: JsonToken;
                    JsonArrayStages: JsonArray;
                    JsonValue: JsonValue;
                    JsonArrayJobs: JsonArray;
                    JsonTokenLoopJobs: JsonToken;
                    JsonObjectLoopJobs: JsonObject;
                    JsonTokenLoopStages: JsonToken;
                    JsonObjectLoopStages: JsonObject;
                    JsonTokenLoopSteps: JsonToken;
                    JsonObjectLoopSteps: JsonObject;
                    JsonArraySteps: JsonArray;
                    InS: InStream;
                    FileName: Text;
                    Msg: Text;
                    UploadFileMsg: Label 'Upload a YAML file';
                begin
                    if UploadIntoStream(UploadFileMsg, '', '', FileName, InS) then begin
                        JsonObject.ReadFromYaml(InS);
                        if JsonObject.Get('stages', JsonToken) then
                            JsonArrayStages := JsonToken.AsArray();
                        foreach JsonTokenLoopStages in JsonArrayStages do begin
                            JsonObjectLoopStages := JsonTokenLoopStages.AsObject();
                            if JsonObjectLoopStages.Get('jobs', JsonTokenLoopStages) then
                                JsonArrayJobs := JsonTokenLoopStages.AsArray();
                            foreach JsonTokenLoopJobs in JsonArrayJobs do begin
                                JsonObjectLoopJobs := JsonTokenLoopJobs.AsObject();
                                if JsonObjectLoopJobs.Get('steps', JsonTokenSteps) then
                                    JsonArraySteps := JsonTokenSteps.AsArray();
                                foreach JsonTokenLoopSteps in JsonArraySteps do begin
                                    JsonObjectLoopSteps := JsonTokenLoopSteps.AsObject();
                                    if JsonObjectLoopSteps.Get('displayName', JsonTokenDisplayName) then begin
                                        JsonValue := JsonTokenDisplayName.AsValue();
                                        if Msg = '' then
                                            Msg := JsonValue.AsText()
                                        else
                                            Msg := Msg + '\' + JsonValue.AsText();
                                    end;
                                end;
                            end;
                        end;
                    end;
                    if Msg <> '' then
                        Message(Msg);
                end;
            }
        }
    }
}

Great, give it a try!!!😁

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL