In this post we’re going to see how we can parse Json files in our Unreal Engine project.

Download the entire source code from my GitHub repo

For this post, I have created an empty project using the 4.18.3 version of the engine and inside my project’s Content folder I created a folder named JsonFiles and placed the following json document:

If you’re running your UE4 project while creating the above file, a popup window will be displayed notifiying you about new assets that you may be interested in importing inside the Editor. In this menu, select the “Don’t import” option. The reason behind this option is that in case we try to import our json file inside the Editor, Unreal Engine will unsuccessfully try to convert our file into either a DataTable or a Curve. Moreover, please note in case the json file is going to be a part of your shipping build, make sure to include the folder we created on the packaging details as well.

Once you have created the mentioned file, go to your project’s .Build.cs file and add the following line (so we can access the json related functions from our code later on):

PublicDependencyModuleNames.AddRange(new string[] { "Json", "JsonUtilities" });

In order to parse and access the data from our json file, we need to do the following:

  1. Get the json file from the saved location in our project
  2. Save its contents in a FString variable
  3. Convert the FString variable to a JsonObject that is supposed by the engine
  4. Use the built-in deserialize functions  for the JsonObject

If the deserialization is completed successfully, we will be able to parse all the required data in a pretty straightforward way.

So, in order to code every aforementioned step, create a new C++ class that inherits the Actor class and add the following code inside the BeginPlay function:

Moreover, make sure to include the JsonUtilies.h header file in your source file. This is needed in order to access the engine’s built-in deserialize functions. At this point, if you compile your code and place your Actors inside a level, you will see the following messages on your output log:

(Click on image to enlarge in a new tab)

For more information regarding the JsonValue and JsonObject classes you should check out the Engine’s source code over here.