How to create a web service that accepts a json with an array?

I already know that the “official” way is sending all the entire JSON in a string with unbound actions, but per project specification I need the API to be able to read json arrays in json format.

To be clear with an example, this is the data to be sent to the API:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"Batch": "IC0001",
"Colors": [
{
"Color": "GENERAL",
"Code": "112",
"Description": "BERMELLON",
"RGB": "255,0,0",
"GroupWeb": "40"
},
{
"Color": "GENERAL",
"Code": "111",
"Description": "ROJERAS",
"RGB": "255,0,0",
"GroupWeb": "40"
}
]
}
</code>
<code>{ "Batch": "IC0001", "Colors": [ { "Color": "GENERAL", "Code": "112", "Description": "BERMELLON", "RGB": "255,0,0", "GroupWeb": "40" }, { "Color": "GENERAL", "Code": "111", "Description": "ROJERAS", "RGB": "255,0,0", "GroupWeb": "40" } ] } </code>
{
    "Batch": "IC0001",
    "Colors": [
        {
            "Color": "GENERAL",
            "Code": "112",
            "Description": "BERMELLON",
            "RGB": "255,0,0",
            "GroupWeb": "40"
        },
        {
            "Color": "GENERAL",
            "Code": "111",
            "Description": "ROJERAS",
            "RGB": "255,0,0",
            "GroupWeb": "40"
        }
    ]
}

I’m trying to read the JSON with the codeunit “JSON Management”:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>codeunit 60201 IntegracionPLM
{
procedure ReadColorJSON(data: Text)
var
JSONManagement: Codeunit "JSON Management";
ArrayJSONManagement: Codeunit "JSON Management";
ObjectJSONManagement: Codeunit "JSON Management";
i: Integer;
JsonArrayText: Text;
ColorJsonObject: Text;
GrupoColoresText: Text;
CodigoText: Text;
DescripcionText: Text;
ValorRGBText: Text;
AgrupacionWebText: Text;
begin
JSONManagement.InitializeObject(Data);
if JSONManagement.GetArrayPropertyValueAsStringByName('data', JsonArrayText) then begin
ArrayJSONManagement.InitializeCollection(JsonArrayText);
for i := 0 to ArrayJSONManagement.GetCollectionCount() - 1 do begin
ArrayJSONManagement.GetObjectFromCollectionByIndex(ColorJsonObject, i);
ObjectJSONManagement.InitializeObject(ColorJsonObject);
ObjectJSONManagement.GetStringPropertyValueByName('GrupoColores', GrupoColoresText);
ObjectJSONManagement.GetStringPropertyValueByName('Codigo', CodigoText);
ObjectJSONManagement.GetStringPropertyValueByName('Descripcion', DescripcionText);
ObjectJSONManagement.GetStringPropertyValueByName('ValorRGB', ValorRGBText);
ObjectJSONManagement.GetStringPropertyValueByName('AgrupacionWeb', AgrupacionWebText);
Message('GrupoColores: %1, Codigo: %2, Descripcion: %3, ValorRGB: %4, AgrupacionWeb: %5',
GrupoColoresText, CodigoText, DescripcionText, ValorRGBText, AgrupacionWebText);
end;
end;
end;
}
</code>
<code>codeunit 60201 IntegracionPLM { procedure ReadColorJSON(data: Text) var JSONManagement: Codeunit "JSON Management"; ArrayJSONManagement: Codeunit "JSON Management"; ObjectJSONManagement: Codeunit "JSON Management"; i: Integer; JsonArrayText: Text; ColorJsonObject: Text; GrupoColoresText: Text; CodigoText: Text; DescripcionText: Text; ValorRGBText: Text; AgrupacionWebText: Text; begin JSONManagement.InitializeObject(Data); if JSONManagement.GetArrayPropertyValueAsStringByName('data', JsonArrayText) then begin ArrayJSONManagement.InitializeCollection(JsonArrayText); for i := 0 to ArrayJSONManagement.GetCollectionCount() - 1 do begin ArrayJSONManagement.GetObjectFromCollectionByIndex(ColorJsonObject, i); ObjectJSONManagement.InitializeObject(ColorJsonObject); ObjectJSONManagement.GetStringPropertyValueByName('GrupoColores', GrupoColoresText); ObjectJSONManagement.GetStringPropertyValueByName('Codigo', CodigoText); ObjectJSONManagement.GetStringPropertyValueByName('Descripcion', DescripcionText); ObjectJSONManagement.GetStringPropertyValueByName('ValorRGB', ValorRGBText); ObjectJSONManagement.GetStringPropertyValueByName('AgrupacionWeb', AgrupacionWebText); Message('GrupoColores: %1, Codigo: %2, Descripcion: %3, ValorRGB: %4, AgrupacionWeb: %5', GrupoColoresText, CodigoText, DescripcionText, ValorRGBText, AgrupacionWebText); end; end; end; } </code>
codeunit 60201 IntegracionPLM
{ 
    procedure ReadColorJSON(data: Text)
    var
        JSONManagement: Codeunit "JSON Management";
        ArrayJSONManagement: Codeunit "JSON Management";
        ObjectJSONManagement: Codeunit "JSON Management";
        i: Integer;
        JsonArrayText: Text;
        ColorJsonObject: Text;
        GrupoColoresText: Text;
        CodigoText: Text;
        DescripcionText: Text;
        ValorRGBText: Text;
        AgrupacionWebText: Text;
    begin
        JSONManagement.InitializeObject(Data);

        if JSONManagement.GetArrayPropertyValueAsStringByName('data', JsonArrayText) then begin
            ArrayJSONManagement.InitializeCollection(JsonArrayText);

            for i := 0 to ArrayJSONManagement.GetCollectionCount() - 1 do begin
                ArrayJSONManagement.GetObjectFromCollectionByIndex(ColorJsonObject, i);
                ObjectJSONManagement.InitializeObject(ColorJsonObject);

                ObjectJSONManagement.GetStringPropertyValueByName('GrupoColores', GrupoColoresText);
                ObjectJSONManagement.GetStringPropertyValueByName('Codigo', CodigoText);
                ObjectJSONManagement.GetStringPropertyValueByName('Descripcion', DescripcionText);
                ObjectJSONManagement.GetStringPropertyValueByName('ValorRGB', ValorRGBText);
                ObjectJSONManagement.GetStringPropertyValueByName('AgrupacionWeb', AgrupacionWebText);

                Message('GrupoColores: %1, Codigo: %2, Descripcion: %3, ValorRGB: %4, AgrupacionWeb: %5', 
                         GrupoColoresText, CodigoText, DescripcionText, ValorRGBText, AgrupacionWebText);
            end;
        end;
    end;
}

But I’m getting this error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"error": {
"code": "BadRequest",
"message": "One or more errors occurred. (One or more errors occurred. (An unexpected 'StartArray' node was found when reading from the JSON reader. A 'StartObject' node was expected.)) CorrelationId: 829669bb-ea16-4703-9ef1-e9431d47928e."
}
}
</code>
<code>{ "error": { "code": "BadRequest", "message": "One or more errors occurred. (One or more errors occurred. (An unexpected 'StartArray' node was found when reading from the JSON reader. A 'StartObject' node was expected.)) CorrelationId: 829669bb-ea16-4703-9ef1-e9431d47928e." } } </code>
{
    "error": {
        "code": "BadRequest",
        "message": "One or more errors occurred. (One or more errors occurred. (An unexpected 'StartArray' node was found when reading from the JSON reader. A 'StartObject' node was expected.))  CorrelationId:  829669bb-ea16-4703-9ef1-e9431d47928e."
    }
}

The request is not going through. Not even reaching my code.

How can I read a json array from my Business Central web service?

5

Solved by following this guide: https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-develop-custom-api

Step 3 did the trick, creating a page with a part:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>page 50100 "API Car Brand"
{
PageType = API;
APIVersion = 'v1.0';
APIPublisher = 'bctech';
APIGroup = 'demo';
EntityCaption = 'Car Brand';
EntitySetCaption = 'Car Brands';
EntityName = 'carBrand';
EntitySetName = 'carBrands';
ODataKeyFields = SystemId;
SourceTable = "Car Brand";
Extensible = false;
DelayedInsert = true;
layout
{
area(content)
{
repeater(Group)
{
field(id; Rec.SystemId)
{
Caption = 'Id';
Editable = false;
}
field(name; Rec.Name)
{
Caption = 'Name';
}
field(description; Rec.Description)
{
Caption = 'Description';
}
field(country; Rec.Country)
{
Caption = 'Country';
}
}
part(carModels; "API Car Model")
{
Caption = 'Car Models';
EntityName = 'carModel';
EntitySetName = 'carModels';
SubPageLink = "Brand Id" = Field(SystemId);
}
}
}
}
</code>
<code>page 50100 "API Car Brand" { PageType = API; APIVersion = 'v1.0'; APIPublisher = 'bctech'; APIGroup = 'demo'; EntityCaption = 'Car Brand'; EntitySetCaption = 'Car Brands'; EntityName = 'carBrand'; EntitySetName = 'carBrands'; ODataKeyFields = SystemId; SourceTable = "Car Brand"; Extensible = false; DelayedInsert = true; layout { area(content) { repeater(Group) { field(id; Rec.SystemId) { Caption = 'Id'; Editable = false; } field(name; Rec.Name) { Caption = 'Name'; } field(description; Rec.Description) { Caption = 'Description'; } field(country; Rec.Country) { Caption = 'Country'; } } part(carModels; "API Car Model") { Caption = 'Car Models'; EntityName = 'carModel'; EntitySetName = 'carModels'; SubPageLink = "Brand Id" = Field(SystemId); } } } } </code>
page 50100 "API Car Brand"
{
    PageType = API;

    APIVersion = 'v1.0';
    APIPublisher = 'bctech';
    APIGroup = 'demo';

    EntityCaption = 'Car Brand';
    EntitySetCaption = 'Car Brands';
    EntityName = 'carBrand';
    EntitySetName = 'carBrands';

    ODataKeyFields = SystemId;
    SourceTable = "Car Brand";

    Extensible = false;
    DelayedInsert = true;

    layout
    {
        area(content)
        {
            repeater(Group)
            {
                field(id; Rec.SystemId)
                {
                    Caption = 'Id';
                    Editable = false;
                }

                field(name; Rec.Name)
                {
                    Caption = 'Name';
                }
                field(description; Rec.Description)
                {
                    Caption = 'Description';
                }
                field(country; Rec.Country)
                {
                    Caption = 'Country';
                }
            }

            part(carModels; "API Car Model")
            {
                Caption = 'Car Models';
                EntityName = 'carModel';
                EntitySetName = 'carModels';
                SubPageLink = "Brand Id" = Field(SystemId);
            }
        }
    }
}

And it’s part:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>page 50101 "API Car Model"
{
PageType = API;
APIVersion = 'v1.0';
APIPublisher = 'bctech';
APIGroup = 'demo';
EntityCaption = 'Car Model';
EntitySetCaption = 'Car Models';
EntityName = 'carModel';
EntitySetName = 'carModels';
ODataKeyFields = SystemId;
SourceTable = "Car Model";
Extensible = false;
DelayedInsert = true;
layout
{
area(content)
{
repeater(Group)
{
field(id; Rec.SystemId)
{
Caption = 'Id';
Editable = false;
}
field(name; Rec.Name)
{
Caption = 'Name';
}
field(description; Rec.Description)
{
Caption = 'Description';
}
field(brandId; Rec."Brand Id")
{
Caption = 'Brand Id';
}
field(power; Rec.Power)
{
Caption = 'Power';
}
field(fuelType; Rec."Fuel Type")
{
Caption = 'Fuel Type';
}
}
}
}
}
</code>
<code>page 50101 "API Car Model" { PageType = API; APIVersion = 'v1.0'; APIPublisher = 'bctech'; APIGroup = 'demo'; EntityCaption = 'Car Model'; EntitySetCaption = 'Car Models'; EntityName = 'carModel'; EntitySetName = 'carModels'; ODataKeyFields = SystemId; SourceTable = "Car Model"; Extensible = false; DelayedInsert = true; layout { area(content) { repeater(Group) { field(id; Rec.SystemId) { Caption = 'Id'; Editable = false; } field(name; Rec.Name) { Caption = 'Name'; } field(description; Rec.Description) { Caption = 'Description'; } field(brandId; Rec."Brand Id") { Caption = 'Brand Id'; } field(power; Rec.Power) { Caption = 'Power'; } field(fuelType; Rec."Fuel Type") { Caption = 'Fuel Type'; } } } } } </code>
page 50101 "API Car Model"
{
    PageType = API;

    APIVersion = 'v1.0';
    APIPublisher = 'bctech';
    APIGroup = 'demo';

    EntityCaption = 'Car Model';
    EntitySetCaption = 'Car Models';
    EntityName = 'carModel';
    EntitySetName = 'carModels';

    ODataKeyFields = SystemId;
    SourceTable = "Car Model";

    Extensible = false;
    DelayedInsert = true;

    layout
    {
        area(content)
        {
            repeater(Group)
            {
                field(id; Rec.SystemId)
                {
                    Caption = 'Id';
                    Editable = false;
                }
                field(name; Rec.Name)
                {
                    Caption = 'Name';
                }
                field(description; Rec.Description)
                {
                    Caption = 'Description';
                }
                field(brandId; Rec."Brand Id")
                {
                    Caption = 'Brand Id';
                }
                field(power; Rec.Power)
                {
                    Caption = 'Power';
                }
                field(fuelType; Rec."Fuel Type")
                {
                    Caption = 'Fuel Type';
                }
            }
        }
    }
}

Then go to Postman or your tool of preference and test this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>POST https://api.businesscentral.dynamics.com/v2.0/<environmentName>/api/bctech/demo/v1.0/companies(<company id>))/carBrands
{
"name": "CARBRAND2",
"description": "Car Brand 2",
"country": "Germany",
"carModels": [{
"name": "MODELA",
"description": "Model A",
"power": 0,
"fuelType": "Electric"
},
{
"name": "MODELB",
"description": "Model B",
"power": 0,
"fuelType": "Electric"
}]
}
</code>
<code>POST https://api.businesscentral.dynamics.com/v2.0/<environmentName>/api/bctech/demo/v1.0/companies(<company id>))/carBrands { "name": "CARBRAND2", "description": "Car Brand 2", "country": "Germany", "carModels": [{ "name": "MODELA", "description": "Model A", "power": 0, "fuelType": "Electric" }, { "name": "MODELB", "description": "Model B", "power": 0, "fuelType": "Electric" }] } </code>
POST https://api.businesscentral.dynamics.com/v2.0/<environmentName>/api/bctech/demo/v1.0/companies(<company id>))/carBrands
{
    "name": "CARBRAND2",
    "description": "Car Brand 2",
    "country": "Germany",
    "carModels": [{
                    "name": "MODELA",
                    "description": "Model A",
                    "power": 0,
                    "fuelType": "Electric"
                },
                {
                    "name": "MODELB",
                    "description": "Model B",
                    "power": 0,
                    "fuelType": "Electric"
                }]
}

I think what you are looking for is an Unbound API action.

https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/devenv-creating-and-interacting-with-odatav4-unbound-action

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật