How to add an Array of Json Objects to a Json Object?

Hi there,

how is it possible to add an array of Json objects to the main Json Object?

I managed to add a value array:

// Json object
TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject);

// Value array
TArray< TSharedPtr<FJsonValue> > ValueArray;

// Create two values and add them to the array
TSharedPtr<FJsonValue> Value1 = MakeShareable(new FJsonValueString("value1"));    
ValueArray.Add(Value1);    
TSharedPtr<FJsonValue> Value2 = MakeShareable(new FJsonValueString("value2"));    
ValueArray.Add(Value2);

// Add the array to the Json object
JsonObject->SetArrayField("array", ValueArray);

However I would like to have the array as JsonObjects. That basically the array would be declared as:

TArray< TSharedPtr  <FJsonObject> > ObjArray;

and I add to the main FJsonObject:

// create JsonObj array
TArray< TSharedPtr<FJsonObject> > ObjArray;

// create a Json object and add a string field
TSharedPtr<FJsonObject> JsonObj = MakeShareable(new FJsonObject);    
JsonObj ->SetStringField("Abc", "AbcValue");

// add the object to the array
ObjArray.Add(JsonObj);

// ***THIS does not work***
JsonObject->SetArrayField("array", ObjArray);

=====EDIT 1 =====

Compile error:

  • error C2664: ‘void FJsonObject::SetArrayField(const FString &,const TArray,FDefaultAllocator> &)’ : cannot convert argument 2 from ‘TArray,FDefaultAllocator>’ to 'const TArray,FDefaultAllocator> &'JsonObject->SetArrayField(“array”, ObjArray);

  • Reason: cannot convert from ‘TArray,FDefaultAllocator>’ to ‘const TArray,FDefaultAllocator>’

  • No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Thanks!

1 Like