Parsing Json array UStruct

Well I have been trying to read my UStruct array JSon data but it always sets the UStruct empty. Here’s my JSon data:

[
    {
        "Sexy little thing":
        {
            "variable1": "Value1",
            "variable2": "Value2",
            "variable3": "Value3",
            "variable4": "Value4"
        }
    },
    {
        "My item":
        {
            "variable1": "Value1",
            "variable2": "Value2",
            "variable3": "Value3",
            "variable4": "Value4"
        }
    }
]

And here’s my last code try:

FString dataOnFile;

	FFileHelper::LoadFileToString(dataOnFile, TEXT("MyBoringPath/Test JSon"));

    //Prints the data correctly
	GLog->Log(dataOnFile);

	//Not working......
	/*TArray<FMyItem> data;
	FJsonObjectConverter::JsonArrayStringToUStruct(dataOnFile, &data, 0, 0);*/

	TArray<TSharedPtr<FJsonValue>> arrayDeserialized;

	TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(dataOnFile);

	if (FJsonSerializer::Deserialize(Reader, arrayDeserialized))
	{
		TArray<FMyItem> data;
		FJsonObjectConverter::JsonArrayToUStruct(arrayDeserialized, &data, 0, 0);

		GLog->Log("Name: " + data[0].variable1);
	}

Now the GLog for the data outputs correctly, and even the arrayDeserialized or the data are returning correct size values (They both return 2). But when I try to read the variable1 it’s empty… Here’s my UStruct:

USTRUCT(BlueprintType)
struct FCraftableItem
{
	GENERATED_BODY()
	
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UProp")
	FString variable1;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UProp")
	FString variable2;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UProp")
	FString variable3;
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "UProp")
	FString variable4;

	FCraftableItem()
	{
	}

	FCraftableItem(FString craftedAt, FString variable1, FString variable3, FString variable4)
	{
		this->variable1= variable1;
		this->variable2= variable2;
		this->variable3= variable3;
		this->variable4= variable4;
	}
};

I tried saving my JsonArray inside a FJsonObject and serializing it, but I get the same results. Am I missing something?

I have the same problem, did you managed to find the solution?

Yes, remove the first { and the last } from the file. I don’t know why but that’s the only way I managed to make it work. It will end looking like this:

[
         "Sexy little thing":
         {
             "variable1": "Value1",
             "variable2": "Value2",
             "variable3": "Value3",
             "variable4": "Value4"
         }
     },
     {
         "My item":
         {
             "variable1": "Value1",
             "variable2": "Value2",
             "variable3": "Value3",
             "variable4": "Value4"
         }
 ]

You needed to add surrounding {} to the whole json, I’m not sure how your solution worked but this is the proper way

Can you share a working example?

Hi, you need more conventional structure like

 {
     "items": [
       {
             "name": "val",
             "money": 5
       },
      {
             "name": "val2",
             "money": 10
       }
      ]
    }

Using this structure possible to automize works with json. This little tool - https://json2ue4.com can generate for you all structures with serialization, just save your time. Enjoy.

You have an open block comment before your code.

/*TArray data;

That would make sense, but that json was made within Unreal not outside.

That’s not why it wasn’t not working, that comment is closed on the next line.