Skip to content

JSON C++ Reference ​

Build and parse JSON strings from C++ with UJSONBlueprintLibrary.

Header: JSONBlueprintLibrary.h · Class: UJSONBlueprintLibrary

Every function works on JSON objects stored as plain FString values. Creation functions return an updated copy of the string, and parsing functions return bool with the value in an output parameter. All functions are static. For detailed behavior and examples, see the JSON nodes.

NOTE

Only JSON objects ({...}) are treated as valid at the top level. When the input string isn't a valid object, the add functions start over with a new, empty object.

Enums ​

cpp
enum class EJSONFieldType : uint8 { String, Number, Boolean };
enum class EJSONArrayType : uint8 { String, Number, Object };
ValueDescription
EJSONFieldType::StringStores the value as a JSON string.
EJSONFieldType::NumberConverts the value with FCString::Atof (single precision). Non-numeric text becomes 0.
EJSONFieldType::BooleanConverts the value with FString::ToBool.
EJSONArrayType::StringEach element is a JSON string.
EJSONArrayType::NumberEach element is converted to a number, as with EJSONFieldType::Number.
EJSONArrayType::ObjectEach element is a JSON object string. Elements that aren't valid objects are skipped.

Creation Functions ​

NameReturnsDescription
AddField(EJSONFieldType FieldType, const FString& JSONString, const FString& FieldName, const FString& Value)FStringAdds or replaces a field, converting Value based on FieldType. See Add Field.
AddArrayField(EJSONArrayType ArrayType, const FString& JSONString, const FString& FieldName, const TArray<FString>& Values)FStringAdds an array field, converting each element based on ArrayType. See Add Array Field.
AddObjectField(const FString& JSONString, const FString& FieldName, const FString& NestedJSON)FStringNests a JSON object. An invalid NestedJSON nests {}. See Add Object Field.
AddNullField(const FString& JSONString, const FString& FieldName)FStringAdds a field with a null value. See Add Null Field.
RemoveField(const FString& JSONString, const FString& FieldName, bool& bSuccess)FStringRemoves a field. bSuccess is true if the field existed. Invalid input is returned unchanged. See Remove Field.
MergeJSON(const FString& JSONString1, const FString& JSONString2)FStringCopies the top-level fields of the second object into the first, overwriting duplicates. See Merge JSON.
PrettyPrintJSON(const FString& JSONString)FStringFormats the JSON with indentation and line breaks. Invalid input is returned unchanged. See Pretty Print JSON.
MinifyJSON(const FString& JSONString)FStringRemoves all formatting whitespace. Invalid input is returned unchanged. See Minify JSON.

Parsing Functions ​

NameReturnsDescription
IsValidJSON(const FString& JSONString)booltrue if the string is a valid JSON object. See Is Valid JSON.
HasField(const FString& JSONString, const FString& FieldName)booltrue if the field exists, including null fields. See Has Field.
IsFieldNull(const FString& JSONString, const FString& FieldName)booltrue if the field exists and is null. See Is Field Null.
GetStringField(const FString& JSONString, const FString& FieldName, FString& OutValue)boolReads a field as a string. See Get String Field.
GetNumberField(const FString& JSONString, const FString& FieldName, float& OutValue)boolReads a field as a float. See Get Number Field.
GetBooleanField(const FString& JSONString, const FString& FieldName, bool& OutValue)boolReads a field as a bool. See Get Boolean Field.
GetArrayField(EJSONArrayType ArrayType, const FString& JSONString, const FString& FieldName, TArray<FString>& OutValues)boolReads an array field, with each element as a string. Returns true if the field exists and is an array. See Get Array Field.
GetObjectField(const FString& JSONString, const FString& FieldName, FString& OutJSON)boolReads a nested object as a JSON string. Returns true if the field exists and is an object. See Get Object Field.
GetAllFieldNames(const FString& JSONString, TArray<FString>& OutFieldNames)boolLists the top-level field names. Returns true if the JSON is valid. See Get All Field Names.

GetStringField, GetNumberField, and GetBooleanField return true whenever the field exists, without checking its type. The output parameter is only written when they return true, so initialize it first.

Example ​

Build a save object, write it to disk with File IO, then read it back:

cpp
#include "JSONBlueprintLibrary.h"
#include "FileIO.h"

void UMySaveSystem::Save(const FString& PlayerName, int32 Level, const TArray<FString>& Inventory)
{
    FString Json;
    Json = UJSONBlueprintLibrary::AddField(EJSONFieldType::String, Json, TEXT("playerName"), PlayerName);
    Json = UJSONBlueprintLibrary::AddField(EJSONFieldType::Number, Json, TEXT("level"), FString::FromInt(Level));
    Json = UJSONBlueprintLibrary::AddArrayField(EJSONArrayType::String, Json, TEXT("inventory"), Inventory);

    FString SavedPath;
    UFileIO::SaveTextToFile(EFileLocation::ProjectSaved, TEXT("SaveGames/slot1.json"), UJSONBlueprintLibrary::PrettyPrintJSON(Json), SavedPath);
}

bool UMySaveSystem::Load(FString& OutPlayerName, int32& OutLevel, TArray<FString>& OutInventory)
{
    FString Json;
    FString LoadedPath;
    if (!UFileIO::LoadFileToString(EFileLocation::ProjectSaved, TEXT("SaveGames/slot1.json"), Json, LoadedPath)
        || !UJSONBlueprintLibrary::IsValidJSON(Json))
    {
        return false;
    }

    float LevelValue = 0.0f;
    UJSONBlueprintLibrary::GetStringField(Json, TEXT("playerName"), OutPlayerName);
    UJSONBlueprintLibrary::GetNumberField(Json, TEXT("level"), LevelValue);
    UJSONBlueprintLibrary::GetArrayField(EJSONArrayType::String, Json, TEXT("inventory"), OutInventory);

    OutLevel = FMath::RoundToInt(LevelValue);
    return true;
}