skip to Main Content

Parsing Json files

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

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.

Avatar photo

This Post Has 3 Comments

  1. Hi,

    Your tuts are very useful.

    I can’t find the way how to set a socket location or transform. We have GetSocketLocation. So how to Set? And how to set socket rotation?

    EDIT:

    The socket of my enemy’s hand should follow the spine bone/socket of my character.

    USkeletalMeshSocket const * EnemyhandR = Enemy1->GetMesh()->GetSocketByName(“hand_r”);
    USkeletalMeshSocket const * MHSpine3 = Enemy1->GetMesh()->GetSocketByName(“spine_03”);

    FVector EnemySLoc = Enemy1->GetMesh()->GetSocketLocation(“hand_r”);
    FVector MHSLoc = MH->GetMesh()->GetSocketLocation(“spine_03”);
    FVector Forward = (MHSLoc – EnemySLoc);
    FRotator SocketRot = FRotationMatrix::MakeFromX(Forward).Rotator();

    And

    EnemyhandR->SetSocketRotation(SocketRot);

    But no SetSocketRotation. How to make an AI bot aim at a player with aim-offset?

  2. I got this working, thanks so much for this tutorial!

    Unreal Engine 4.14.3

    cpp:
    #include “Json.h”

    Build.cs:
    PublicDependencyModuleNames.AddRange(new string[] { “Json” });

    Note:
    – Build and run from cpp editor
    – if your json file doesn’t have object label(s), then use the full JsonObject in this code to parse through the field labels.
    – No need to include JsonUtilities.h, doesn’t cause problems if you do though.

  3. All of this can be done in a few lines now in ue5

    FString JsonString;
    FFileHelper::LoadFileToString(JsonString, *FilePath);

    TArray Items;
    if(FJsonObjectConverter::JsonArrayStringToUStruct(JsonString, &Items))
    {
    //loop thru items and do stuff
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top