Describe the bug
Array of JsonObject doesn't work as expected. The entire array shows the same value which corresponds with the last value being assigned to one of the indexes (slots).
To Reproduce
Steps and to reproduce the behavior:
MyArray[1].ReadFrom('{"Index1":"Index 1"}'); makes the last value visible in all indexes (slots) directly. local procedure ArrayOfJsonObjectTest01()
var
MyArray: Array[3] of JsonObject;
begin
MyArray[1].ReadFrom('{"Index1":"Index 1"}');
MyArray[2].ReadFrom('{"Index2":"Index 2"}');
MyArray[3].ReadFrom('{"Index3":"Index 3"}');
end;
MyObject.ReadFrom('{"Index1":"Index 1"}'); MyArray[1] := MyObject; works more or less in the same way but there is some difference too. The debugger shows the last value only in the indexes being already assigned. So if you set the breakpoint to MyObject.ReadFrom('{"Index3":"Index 3"}'); you will notice that only MyArray[1] and MyArray[2] have the values (and this is always the same, the last one => {"Index2":"Index 2"}). local procedure ArrayOfJsonObjectTest02()
var
MyArray: Array[3] of JsonObject;
MyObject: JsonObject;
begin
MyObject.ReadFrom('{"Index1":"Index 1"}');
MyArray[1] := MyObject;
MyObject.ReadFrom('{"Index2":"Index 2"}');
MyArray[2] := MyObject;
MyObject.ReadFrom('{"Index3":"Index 3"}');
MyArray[3] := MyObject;
end;
local procedure ListOfJsonObjectTest01()
var
MyArray: List of [JsonObject];
MyObject: JsonObject;
begin
MyObject.ReadFrom('{"Index1":"Index 1"}');
MyArray.Add(MyObject);
MyObject.ReadFrom('{"Index2":"Index 2"}');
MyArray.Add(MyObject);
MyObject.ReadFrom('{"Index3":"Index 3"}');
MyArray.Add(MyObject);
Message(format(MyArray.Count));
MyArray.Get(1, MyObject);
Message('Index 1: %1', MyObject);
MyArray.Get(2, MyObject);
Message('Index 2: %1', MyObject);
MyArray.Get(3, MyObject);
Message('Index 3: %1', MyObject);
end;
Expected behavior
I believe, this is an error and should be either fixed or the compiler should produce an error if this is considered as an unsupported scenario. I am aware of this limitation for the List approach but in this case (List of [Blob]), the compiler gives the error. I don't see any details or relevant limitations for Array of.
Using of JsonArray works correctly, of course. But I still believe the problem described above should be solved somehow or maybe we are missing some details which would be very nice to know :)
Screenshots
Ad 1)

Ad 2)

Ad 3)

5. Versions:
This is by design. See the note in the documentation related to JSON types being reference types not value types https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/jsonobject/jsonobject-data-type
Sorry, I beg you to reconsider this. There is one big difference between a reference type and an array of references.
local procedure Test()
var
TestObject: array[5] of JsonObject;
TestText: Text;
begin
TestObject[1].ReadFrom('{"foo":"bar"}');
TestObject[2].ReadFrom('{"hello":"world"}');
TestObject[1].WriteTo(TestText);
Message('%1', TestText);
end;
Those five elements in this array are different references. They cannot all be one.
I might have gone over the issue too quickly. We'll take another look.
Thanks for reconsidering ;)
Most helpful comment
I might have gone over the issue too quickly. We'll take another look.