How to include a json file into build?

Hello all, I have a .json file with critical data to run my game, how do I include such file into the packaged game and how do I reference it from the c++ trying to access it? (file path) I know that the packaging settings have the option to add non asset directories but I’m unsure how this works (asset must be on the content folder and this is not allowed by the editor :S ).

So far this is my approach,

void UUnrealShooterDataSingleton::ParseJSON()
{
	FString JsonString;
	const FString fileName = "D:/Projects/UnrealProjects/UnrealShooter/Shared/UnrealShooterData.json";
	FFileHelper::LoadFileToString(JsonString, *fileName);
	TSharedPtr<FJsonObject> JsonObject;
	TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(JsonString);

	if (FJsonSerializer::Deserialize(Reader, JsonObject))
	{
		const TArray<TSharedPtr<FJsonValue>> SequencesJSON = JsonObject->GetArrayField(TEXT("sequences"));
		const TArray<TSharedPtr<FJsonValue>> WavesJSON = JsonObject->GetArrayField(TEXT("waves"));
		const TArray<TSharedPtr<FJsonValue>> TargetsJSON = JsonObject->GetArrayField(TEXT("targets"));
		const TArray<TSharedPtr<FJsonValue>> LocationsJSON = JsonObject->GetArrayField("locations");

		UUnrealShooterDataSingleton::ParseLocations(LocationsJSON);
		UUnrealShooterDataSingleton::ParseTargets(TargetsJSON);
		UUnrealShooterDataSingleton::ParseWaves(WavesJSON);
		UUnrealShooterDataSingleton::ParseSequences(SequencesJSON);
	}
}

Note that the file path is absolute and it’s saved outside the content folder, this is because the editor won’t let me add a .json file without converting it into a data table/data curve.

2 Likes
  • Create new folder (like Data) inside your Content folder and place your .json file inside

  • Ignore the editor converting the file (the original .json should still be where you put it).

  • Inside project settings add your Data folder to the list of folders to copy (if you add it to the packaged it should only embed the .uasset file into game packages, which you don’t want)

  • Use the FPaths class and it’s static methods to reference the folder, like so:

    FString path = FPaths::Combine(*FPaths::GameDir(), *FString(“Data”));
    Note: Not at my dev machine at the moment so haven’t tested that code - it might need adjusting

1 Like

Oh man thanks so much, I was about to start digging into these data tables and data assets to see how to migrate my content from the .json file, I’ll try this when I get home ^^

Edit: great man it works! but I had to use something more like:
FPaths::GameContentDir() + “/Game/Data/myData.json”;

1 Like