How do I loop through a JSON Array that has sub-Arrays

I’ve been battling with the Unreal and the following JSON for a few days now, it’s an array (“state”) which has 2 sub-arrays, each containing a few JSON Object.

Screenshot_21

Using the JSON Utilities plugins I’ve tried accessing the individuals JSON object, using “For Each Loop” nodes and others “Array” nodes, this thread really helped me get on the right path to solve this, but so far and with many different attempts, I either have no results, or only the first JSON Object of each sub-array.

Here is the JSON String ↓

{“state”:[[{“id”:134,“isAlive”:true},{“id”:3445,“isAlive”:true},{“id”:32,“isAlive”:true},{“id”:422,“isAlive”:true}],[{“id”:150,“isAlive”:false},{“id”:2502,“isAlive”:true},{“id”:780,“isAlive”:true},{“id”:1345,“isAlive”:true},{“id”:2320,“isAlive”:true},{“id”:555,“isAlive”:false}]]}

(You may need to remove spaces if you copy/paste it, may be common knowledge but I’m a JSON newbie).


Screenshot_20

I believe I should need 2 “For Each Loop” nodes to iterate through the Array, then each subarray. Right now I kind of understand why it’s not working, but I can’t find an alternative that works.

Any help would be really appreciated, thanks for reading.

Blueprint support for JSON is clunky af, it’s probably not possible. Only the JsonObject type is exposed and that’s not gonna be enough to handle this case. Array of arrays are not supported in blueprints.

You’re gonna need C++ in some way or another. Either make your own function or find a better JSON plugin.

	FString JsonString = "\
{\
	\"state\":[\
		[\
			{\"id\":134,\"isAlive\":true},\
			{\"id\":3445,\"isAlive\":true},\
			{\"id\":32,\"isAlive\":true},\
			{\"id\":422,\"isAlive\":true}\
		],[\
			{\"id\":150,\"isAlive\":false},\
			{\"id\":2502,\"isAlive\":true},\
			{\"id\":780,\"isAlive\":true},\
			{\"id\":1345,\"isAlive\":true},\
			{\"id\":2320,\"isAlive\":true},\
			{\"id\":555,\"isAlive\":false}\
		]\
	]\
}";
	TSharedPtr<FJsonObject> JsonObject;
	TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(JsonString);
	if (FJsonSerializer::Deserialize(Reader, JsonObject))
	{
		TArray<TSharedPtr<FJsonValue>> State = JsonObject->GetArrayField("state");

		TArray<TSharedPtr<FJsonValue>> FirstSubarray = State[0]->AsArray();
		UE_LOG(LogTemp, Log, TEXT("First Subarray: %i elements"), FirstSubarray.Num());
		for (TSharedPtr<FJsonValue>& Item : FirstSubarray)
		{
			TSharedPtr<FJsonObject> ItemObj = Item->AsObject();
			UE_LOG(LogTemp, Log, TEXT("- id=%i isAlive=%i"), ItemObj->GetIntegerField("id"), ItemObj->GetBoolField("isAlive") ? 1 : 0);
		}

		TArray<TSharedPtr<FJsonValue>> SecondSubarray = State[1]->AsArray();
		UE_LOG(LogTemp, Log, TEXT("Second Subarray: %i elements"), SecondSubarray.Num());
		for (TSharedPtr<FJsonValue>& Item : SecondSubarray)
		{
			TSharedPtr<FJsonObject> ItemObj = Item->AsObject();
			UE_LOG(LogTemp, Log, TEXT("- id=%i isAlive=%i"), ItemObj->GetIntegerField("id"), ItemObj->GetBoolField("isAlive") ? 1 : 0);
		}
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("Failed to deserialize JSON"));
	}
1 Like

hi,
like @Chatouille said, you better be off creating your own c++ json reader. the plugin that comes with ue5 is very, very basic… if you have an json object array it wont read the file or string, which you can do in the plugin… i also tried hours… sorry to say :frowning:

cheers :vulcan_salute:

I’m going to necro this old post because it’s still coming up in Google results, so is still relevant many years later.

JSON support in Blueprints seems to be pretty full-featured now in 5.3, at least in terms of reading from an arbitrary well-formed JSON payload.

If you’re using a web-based API to acquire your JSON data, you’ll take advantage of the relatively new HTTP Request Node (not the Proxy function). You must enable (for 5.3 anyway) the HTTPBlueprint plugin from Epic, which should be bundled (since at least 5.1).

You’ll use a LoadJSONFromString node to parse the actual data. Set a breakpoint and use the debugger to ensure that you’re getting what you expect here before going any further I guess.

The most important concept is to create all the STRUCTS you need to replicate (exactly) your JSON structure. Here are some pointers:

  1. Start from the deepest nested structure and work your way out. This is because as you create some of those outer structures, you’ll want to set some of the values to other structures. They must already be created in order to do so. So look through your JSON Schema and determine the deepest nested structures and replicate them first in Unreal

  2. Objects are Maps in Unreal. Specifically: JSON Objects that do not have predefined properties (ie: the keys are variable) get turned into String Maps. String Maps have keys that are strings but can still use a STRUCT as a value (see #1 above)

  3. Don’t forget you can iterate through a Map via its Keys array.

  4. You can easily have Arrays of Structs as a variable. It’s almost inevitable if you’re dealing with a big JSON payload that some of it will be an array. If it’s an array of scalar values, great, just choose that value type and set it to Array. But if the value is complex, it’s a STRUCT and you can choose that struct as the value type and then set it to an Array of that struct type.

  5. And this is life-changing, once you are dealing with a struct, remember that you can right-click the Output pin and “Split Struct” which then gives you all the pins corresponding to all the properties of your struct! It’s incredibly powerful and gives you direct access to every part of your JSON data.

Hope this helps.

2 Likes

Man, even i am stuck with the same, any update on this OP?

@dot.tominator gave a perfect answer

also for anyone wondering: you can always write your own parser in Bluperints using node “parse into array”

in ancient version of UE I made JSON parsing only using this node even with nested structures

you could also use built in plugin for JSON to get basic non-nested fields with arrays and then parse those using “parse into array”

i.e. make your own, can be done in BPs or try to learn basics of C++, which is also the way