Business central blog
Subscribe to blog and get news about new posts.

API Object Generator XML/JSON

Welcome to my blog, where today I am excited to introduce you to a new tool that will make a developer's life easier when working with APIs in Business Central. The API Object Generator is a tool that simplifies the process of generating objects based on XML or JSON data from API. As many of you are likely aware, working with XML and JSON data in Business Central can be a time-consuming process. However, the API Object Generator automates the generation of objects based on the information contained within these formats, which saves developers a significant amount of time and effort. By automating the generation of objects, developers can focus on more important tasks, such as designing and implementing APIs that meet the needs of their clients.

App Overview

The idea of the application is to read incoming JSON/XML files and generate AL basic objects from that information. You can paste JSON/XML direclty to page or application can also make HTTP requests with authorization depending on the settings. At the moment the supported authorization types are: Bearer Token, Basic, OAuth1.0, OAuth 2.0. So if you are interested in implementing these authorizations from Business Central in 2023, you can check out this code.

case APIHeader."Auth Type" of
    APIHeader."Auth Type"::"OAuth 1.0", APIHeader."Auth Type"::"OAuth 2.0":
        begin
            RequestAccessToken(APIHeader, ErrorContent, AccessToken);
            Client.DefaultRequestHeaders().Add(
                'Authorization',
                'Bearer ' + AccessToken);
        end;
    APIHeader."Auth Type"::"Bearer Token":
        begin
            Client.DefaultRequestHeaders().Add(
                'Authorization',
                'Bearer ' + APIHeader.GetClientSecret());
        end;
    APIHeader."Auth Type"::Basic:
        begin
            Client.DefaultRequestHeaders().Add(
                'Authorization',
                'Basic ' + Base64Convert.ToBase64(StrSubstNo('%1:%2', APIHeader.Login, APIHeader.GetClientSecret())));
        end;
end;

procedure RequestAccessToken(var APIHeader: Record "AOG API Header"; var ErrorContent: Text; var AccessToken: Text) IsConnected: Boolean
var
    OAuth2: Codeunit OAuth2;
    ListOfScopes: List of [Text];
begin
    ListOfScopes.Add(APIHeader.Scope);

    case APIHeader."Grant Type" of
        APIHeader."Grant Type"::"Authorization Code":
            IsConnected := OAuth2.AcquireTokenByAuthorizationCode(APIHeader."Client ID", APIHeader.GetClientSecret(),
                APIHeader."Authorization URL", APIHeader."Redirect URL", ListOfScopes, Enum::"Prompt Interaction"::None,
                AccessToken, ErrorContent);
        APIHeader."Grant Type"::"Client Credentials":
            case APIHeader."Auth Type" of
                APIHeader."Auth Type"::"OAuth 1.0":
                    begin
                        IsConnected := OAuth2.AcquireTokenWithClientCredentials(APIHeader."Client ID", APIHeader.GetClientSecret(),
                            APIHeader."Authorization URL", APIHeader."Redirect URL",
                            AccessToken, ErrorContent)
                    end;
                APIHeader."Auth Type"::"OAuth 2.0":
                    begin
                        IsConnected := OAuth2.AcquireTokenWithClientCredentials(APIHeader."Client ID", APIHeader.GetClientSecret(),
                            APIHeader."Authorization URL", APIHeader."Redirect URL", ListOfScopes, AccessToken);
                    end;
            end;
    end;
    if not IsConnected then
        Error(GetLastErrorText());
end;
I also prepared two test examples, which are loaded when you open the page "API Object Generator". You can click "Generate Objects" to test them. For you will be prepared archive in which you will find table for storing values and codeunit with already ready function for writing this information to the table.
Here is sample of JSON setup from HTTP without auth:
Briefly about the most important fields:
  • Name - is used to name result Table and Codeunit
  • Generation Type - "Raw Data" to paste JSON/XML directly, "API" to read data from API
  • Data Format - format of source data
  • Auth Type - authrization type in case of API "Generation Type"
  • Base Prefix - prefix of generated objects
  • Base ID - id of generated objects
  • Root Path - JsonPath or XPath to root node/token, from that level all fields will be used any other level will be ignored for now.
  • Response - in case of API "Generation Type" you will see response data of http request.
  • Request Data - in case of API "Generation Type" you can paste here any request data to send with request e.g. json/xml/text
  • Request Content Type - in case of API "Generation Type" and if "Request Data" is not empty you can use it to select correct content type of http request e.g. "text/plain", "application/json"
Source code and application is available on github:
https://github.com/Drakonian/api_object_generator_json_xml
May 3, 2023