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:
{
"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":
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:
{
"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?
dataparameter in ReadColorJson function contains what you need, then you only have to parse it properly. Json Management codeunit is kind of overloaded imho. Just use built-in json types like JsonObject and JsonArray.