How do I properly utilize JsonObjectStringToUStruct?

I’m trying to take an object from a json http response and convert it to a UStruct. I came across the JsonObjectStringToUStruct function via JsonUtilities.h. While it looks like the solution to my problem, I’m having some issues with it. I haven’t worked in c++ in forever, so please forgive me for any syntax issues. Anyway, I’m not exactly sure how to properly supply the parameters needed by this procedure. Specifically the structure definition UStruct. I’m assuming this is for mapping parameters from json into the corresponding properties of the output struct, but I’m not sure what that is supposed to look like.

Any help would be greatly appreciated. Here is the basics of what I have so far.

const UStruct *structDefinition = {
    // What do I do with this.. if anything? 
    		};
 
    FTestObjectStruct testObjectStructOut = FTestObjectStruct();
    
    bool bSomePropertiesWereWritten = FJsonObjectConverter::JsonObjectStringToUStruct(jsonResponseString, structDefinition, &testObjectStructOut, 0, 0);

bump. Still stuck. Any suggestions.

The part you’re missing is that the struct definition is a UStruct* object, which is generated as part of the automatic code generation you get when you declare something as USTRUCT in a header file. Here’s an example of a UStruct definition from one of out header files

USTRUCT()
struct FNotificationLevelUp
{
	GENERATED_USTRUCT_BODY()

	/** The new level */
	UPROPERTY()
	int32 Level;
    
	FNotificationLevelUp()
		: Level(-1)
	{
	}
};

And here’s a simple example of how to use this, where we are converting a json string into a UStruct that’s allocated on the stack:

FNotificationLevelUp LevelUpData;
FJsonObjectConverter::JsonObjectStringToUStruct(JsonStr, LevelUpData.StaticStruct(), &LevelUpData, 0, 0)

In both examples the important part is that the field names of the UStruct match the names of the json parameters. Our JSON is using java case conventions but UProperties are case insensitive so there’s some hacky code to convert field name case.

1 Like

Thank you very much Ben, that worked like a charm. I had everything setup correctly but wasn’t aware of the StaticStruct function. I can’t believe I didn’t notice it w/ Intellitype. I was soooooo close. Thanks again!

This SHOULD be in the docs! StaticStruct() is a kind of a ‘secret knowledge’ as it is now. Thank you, Ben!

It seems like the above code no longer works in 2024. Instead I need to give the function template parameter like below.

FNotificationLevelUp LevelUpData;
FJsonObjectConverter::JsonObjectStringToUStruct<FNotificationLevelUp>(JsonStr, &messageObject)
1 Like