How to retrieve single text field from Json?

Greetings all,

Currently I have got a json file from the Met Office printed in its entirety to the screen as a string but I wish to be able to print selected values from the json to the screen. As the json is nested I have constructed USTRUCTS to store the converted values to. For example:

USTRUCT()
struct FJsonData
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY()
	FSiteRep SiteRep;

	UPROPERTY()
	FWx Wx;

	UPROPERTY()
	TArray<FParam> Param;

	UPROPERTY()
	FDV DV;

	UPROPERTY()
	FLocation Location;

	UPROPERTY()
	TArray<FPeriod> Period;

	UPROPERTY()
	FRep Rep;
};

This is the main USTRUCT which contains the other USTRUCTs. The other USTRUCTs contain FString variables to store the values from the JsonString.

My problem lies in that my UStructs are empty and I am uncertain how to populate them with the relevant values from the JSonObject. Here is my method so far.

void AHttpActor::OnResponseReceived(FHttpRequestPtr Request, FHttpResponsePtr Response, bool bWasSuccessful)
{
	//Create a pointer to hold the json serialized data
	TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
	
	//Store Json as string
	FString JsonString  = Response->GetContentAsString();
	
	//Create a reader pointer to read the json data
	TSharedRef<TJsonReader<TCHAR>> Reader = TJsonReaderFactory<TCHAR>::Create(JsonString);

	//Deserialize the json data given Reader and the actual object to deserialize
	if (FJsonSerializer::Deserialize(Reader, JsonObject))
	{
		//Convert JsonObjectString to UStruct
		FJsonData JsonData;
		FJsonObjectConverter::JsonObjectStringToUStruct<FJsonData>(
			JsonString,
			&JsonData,
			0, 0);
		
		//Get the value of the json object by field name
		JsonData.Location.country = JsonObject->GetStringField(TEXT("country"));

		//Outputs Json as a string
		GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Yellow, FString(JsonString));
		//Outputs nothing
		GEngine->AddOnScreenDebugMessage(5, 5.0f, FColor::Green, FString(JsonData.Location.country));
	}
	else
	{
		GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Red, FString("error"));
	}
}

Hit a blocker with this and was hoping for some assistance; any help would be appreciated.

I’ve added quite a bit of details to this question: https://answers.unrealengine.com/questions/81533/how-do-i-perform-basic-json-operations-loop-proper.html

It gives an overview and simple example from start to finish on how to work with Json. Maybe it will solve your problem.

Thanks for the reply, although it wasn’t what I was looking for it encouraged me to keep going. My question was a little bit unspecific and it turned out my main problem was that I couldn’t access the nested objects within the JSON. I will post my solution in the answer box in case someone else comes looking.

Ok first things first forget about the USTRUCTS and FJsonObjectConverter for now.

Here is my simple solution:

    //Declare json as string variable
	FString JsonString = Response->GetContentAsString();
	//Create a pointer to hold the json serialized data
	TSharedPtr<FJsonObject> JsonObject = MakeShareable(new FJsonObject());
	//Create a reader pointer to read the json data
	TSharedRef<TJsonReader<TCHAR>> JsonReader = TJsonReaderFactory<TCHAR>::Create(JsonString);

	//Deserialize the json data given Reader and the actual object to deserialize
	if (FJsonSerializer::Deserialize(JsonReader, JsonObject) && JsonObject.IsValid())
	{
		TSharedPtr<FJsonObject> objSiteRep = JsonObject->GetObjectField("SiteRep");
		TSharedPtr<FJsonObject> objDV = objSiteRep->GetObjectField("DV");
		TSharedPtr<FJsonObject> objLocation = objDV->GetObjectField("Location");
		FString placeName = objLocation->GetStringField("name");
		FString country = objLocation->GetStringField("country");

		//Outputs place name as a string in engine
		GEngine->AddOnScreenDebugMessage(5, 5.0f, FColor::Green, FString(name));
		//Outputs country as a string in engine
		GEngine->AddOnScreenDebugMessage(1, 5.0f, FColor::Green, FString(country));
	}
	else
	{
		GEngine->AddOnScreenDebugMessage(1, 2.0f, FColor::Red, FString("error, failed to deserialize"));
	}

It is far from elegant but it works for the little data I am using it for. The object arrays were more fun but I will not clutter up this space with them unless someone asks.

Hope this helps someone.