TArray<FMyStruct> to JSON String

Hi all… I’ve been searching for an a post that already answers this but not having any luck.
I have a need to work with JSON data. I’ve been able to use the JSON utilities for most cases however, there is one case that I’m not finding what I need exactly…

I have these two structures, the first struct being consumed in the second struct.

USTRUCT(BlueprintType, Category = "REST API")
struct FRestAPI_TestName
{
    GENERATED_BODY()

public:

    UPROPERTY(BlueprintReadWrite, Category = "REST API")
    FString testName = "";
};


USTRUCT(BlueprintType, Category = "REST API")
struct FRestAPI_Test_Category
{
    GENERATED_BODY()

public:

    UPROPERTY(BlueprintReadWrite, Category = "REST API")
    FString categoryName = "";

    UPROPERTY(BlueprintReadWrite, Category = "REST API")
    TArray<FRestAPI_TestName> testNames;
};

I can use the UStructToJsonObjectString() no problem with an instance of a FRestAPI_Test_Category instance.

However, I need to be able to convert a TArray<FRestAPI_Test_Category> into a JSON string… I have a function body that looks similar to this.

void MyClass::SomeFunc( TArray<FRestAPI_Test_Category> testCategories )
{
              FString postBody;
              if( FJsonObjectConverter::UStructToJsonObjectString( testCategories, postBody) )
              {
                            ...Do something... 

              }

}

I get a compile time error, I’m assuming it’s because the UStructToJsonObjectString can’t handle a TArray as an input parameter. I’m not finding a function that can take a TArray of structs and covert that.

So, I ended up having to create a utility function that will iterate over each element in the array, convert each FRestAPI_Test_Category struct and append it to a master string, and then also manipulate some of the brackets and what not to make it a properly formatted JSON string…

FString MyClass::TArrayToJSONString(TArray<FRestAPI_Test_Category> testCategories )
{
       FString json = "[";

       for (int x = 0; x < tests.Num(); x++)
       {
              FString postBody;
              FJsonObjectConverter::UStructToJsonObjectString( tests[x], postBody );
              json += postBody;

              // Dont put the comma at the end of the last struct.
              if ((x + 1) != tests.Num())
              {
                     json += ",\n";
              }
       }
       json += "]";
       return json;
}

Is there a better approach than having to manually create my own utility function? Seems like there would have to be some kind of a converstion function that can already handle a TArray of FStruct types.

Thanks for any help.

If the TArray was within a UStruct then you could use the same function to parse it to a string directly right?

That is correct…

If I just pass it a single instance of the parent FStruct, it has no problem parsing the TArray in the child struct…

So if I did something like this…

void MyClass::SomeFunc( FRestAPI_Test_Category testCategory )
{
              FString postBody;
              if( FJsonObjectConverter::UStructToJsonObjectString( testCategory, postBody) )
              {
                            ...Do something... 
              }

}

That does work… Just not sure how to pull off working with a full TArray of them.

Yeah my suggestion is to create a new struct containing your TArray and using that instead of the TArray itself.

For example:

struct FNewStruct
{
         TArray<FRestAPI_Test_Category> testCategories;
};

void MyClass::SomeFunc( FNewStruct testStruct )
{
         FString postBody;
         if( FJsonObjectConverter::UStructToJsonObjectString( testStruct, postBody) )
         {
                 ...Do something... 
         }
}

Hi MonsOlympus…

Thanks for the suggestion. I did try that, and it seemed to fix the problem I was having, however, it seemed to create some other problem. I can’t recall off the top of my head what it was though.

I’ll try this approach again and see what happens.