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
enum class EJSONFieldType : uint8 { String, Number, Boolean };
enum class EJSONArrayType : uint8 { String, Number, Object };| Value | Description |
|---|---|
EJSONFieldType::String | Stores the value as a JSON string. |
EJSONFieldType::Number | Converts the value with FCString::Atof (single precision). Non-numeric text becomes 0. |
EJSONFieldType::Boolean | Converts the value with FString::ToBool. |
EJSONArrayType::String | Each element is a JSON string. |
EJSONArrayType::Number | Each element is converted to a number, as with EJSONFieldType::Number. |
EJSONArrayType::Object | Each element is a JSON object string. Elements that aren't valid objects are skipped. |
Creation Functions
| Name | Returns | Description |
|---|---|---|
AddField(EJSONFieldType FieldType, const FString& JSONString, const FString& FieldName, const FString& Value) | FString | Adds 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) | FString | Adds an array field, converting each element based on ArrayType. See Add Array Field. |
AddObjectField(const FString& JSONString, const FString& FieldName, const FString& NestedJSON) | FString | Nests a JSON object. An invalid NestedJSON nests {}. See Add Object Field. |
AddNullField(const FString& JSONString, const FString& FieldName) | FString | Adds a field with a null value. See Add Null Field. |
RemoveField(const FString& JSONString, const FString& FieldName, bool& bSuccess) | FString | Removes a field. bSuccess is true if the field existed. Invalid input is returned unchanged. See Remove Field. |
MergeJSON(const FString& JSONString1, const FString& JSONString2) | FString | Copies the top-level fields of the second object into the first, overwriting duplicates. See Merge JSON. |
PrettyPrintJSON(const FString& JSONString) | FString | Formats the JSON with indentation and line breaks. Invalid input is returned unchanged. See Pretty Print JSON. |
MinifyJSON(const FString& JSONString) | FString | Removes all formatting whitespace. Invalid input is returned unchanged. See Minify JSON. |
Parsing Functions
| Name | Returns | Description |
|---|---|---|
IsValidJSON(const FString& JSONString) | bool | true if the string is a valid JSON object. See Is Valid JSON. |
HasField(const FString& JSONString, const FString& FieldName) | bool | true if the field exists, including null fields. See Has Field. |
IsFieldNull(const FString& JSONString, const FString& FieldName) | bool | true if the field exists and is null. See Is Field Null. |
GetStringField(const FString& JSONString, const FString& FieldName, FString& OutValue) | bool | Reads a field as a string. See Get String Field. |
GetNumberField(const FString& JSONString, const FString& FieldName, float& OutValue) | bool | Reads a field as a float. See Get Number Field. |
GetBooleanField(const FString& JSONString, const FString& FieldName, bool& OutValue) | bool | Reads a field as a bool. See Get Boolean Field. |
GetArrayField(EJSONArrayType ArrayType, const FString& JSONString, const FString& FieldName, TArray<FString>& OutValues) | bool | Reads 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) | bool | Reads 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) | bool | Lists 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:
#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;
}