# Parse nested json to TMap in Unreal Engine 4

**URL:** <https://forums.unrealengine.com/t/parse-nested-json-to-tmap-in-unreal-engine-4/157998>\
**Category:** C++\
**Tags:** unreal-engine\
**Created:** [February 17, 2021, 12:22am UTC](https://forums.unrealengine.com/t/parse-nested-json-to-tmap-in-unreal-engine-4/157998 "2021-02-17T00:22:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Panmigo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Panmigo](https://forums.unrealengine.com/u/Panmigo)\
**Post date:** [February 17, 2021, 12:22am UTC](https://forums.unrealengine.com/t/parse-nested-json-to-tmap-in-unreal-engine-4/157998/1 "2021-02-17T00:22:32Z")

</div>

I have a nested json with the following structure within UE4:

```auto

{
  "boundary_points __per__ cell": {
    "0": 
      {
        "x": 16.451330042628662,
        "y": 2.0813326861577965
      },
      {
        "x": 16.755262971791506,
        "y": 2.0406535171257136
      }
    ],
    "1": 
      {
        "x": -1.6378002918100634,
        "y": 4.9689057223409412
      },
      {
        "x": 0.9452331911724825,
        "y": 6.1469903490163311
      }
    ]
  }
}

```

I tried using proposed techniques from other threads that suggest using the JsonObjectStringToUStruct. In particular I have the following structs to parse the json logic:

```auto

USTRUCT()
struct FBoundaryPoint
{
  GENERATED_USTRUCT_BODY()
    UPROPERTY()
  int32 x;
  UPROPERTY()
  int32 y;
  
};

USTRUCT()
struct FBoundaryPoints
{
  GENERATED_USTRUCT_BODY()
    UPROPERTY()
  TArray<FBoundaryPoint> BoundaryPoints;
  
};

USTRUCT()
struct FVoronoiStruct
{
  GENERATED_USTRUCT_BODY()
    UPROPERTY()
  TMap<FString, FBoundaryPoints> boundary_points __per__ cell;
};

```

However, Iam unable to find a solution since, the parsed objects return only the keys of the dictionary (“0”. “1”) without parsing correctly the array of elements.

---

<div class="post-metadata">

**Author:** ![Kris](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/kris/32/758129_2.png) [@Kris](https://forums.unrealengine.com/u/Kris)\
**Post date:** [February 19, 2021, 3:06am UTC](https://forums.unrealengine.com/t/parse-nested-json-to-tmap-in-unreal-engine-4/157998/2 "2021-02-19T03:06:49Z")

</div>

Manually doing it not an option?

```auto

TSharedPtr<FJsonObject> FileAsJson;

TArray<TSharedPtr<FJsonValue> > Points = FileAsJson->GetArrayField(TEXT("boundary_points __per__ cell")));
for(int32 i = 0; i < Points.Num() ++i)
{
    TSharedPtr<FJsonObject>& PointsObject = Points*->AsObject();
    FVector2D Coords;
    if (PointsObject->HasTypedField<EJson::Number>(TEXT("x")))
    {
        Coords.X = PointsObject->TryGetNumberField(TEXT("x"));
    }
    if (PointsObject->HasTypedField<EJson::Number>(TEXT("y")))
    {
        Coords.Y = PointsObject->TryGetNumberField(TEXT("y"));
    }
    etc etc
}

```

---

<div class="post-metadata">

**Author:** ![Panmigo](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Panmigo](https://forums.unrealengine.com/u/Panmigo)\
**Post date:** [March 23, 2021, 3:02pm UTC](https://forums.unrealengine.com/t/parse-nested-json-to-tmap-in-unreal-engine-4/157998/3 "2021-03-23T15:02:09Z")

</div>

Yes unfortunately I had to switch to a manual parsing and everything worked as expected. Thanks

---

<div class="post-metadata">

**Author:** ![highlordzombie](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@highlordzombie](https://forums.unrealengine.com/u/highlordzombie)\
**Post date:** [June 22, 2023, 7:00am UTC](https://forums.unrealengine.com/t/parse-nested-json-to-tmap-in-unreal-engine-4/157998/4 "2023-06-22T07:00:49Z")

</div>

If anyone is struggling with nested Jsons and stumbles on this thread, I’ll point out that the above solution is not 100% correct because “_boundary\_points\_\_per\_\_cell_” is not referring to an array. It is referring to another dict.

So using the following will give you an error!

> TArray\<TSharedPtr \> Points = FileAsJson-\>GetArrayField(TEXT(“boundary\_points\_\_per\_\_cell”)));

What helped me here was using the [Values](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Json/Dom/FJsonObject/Values/) member variable in the [FJsonObject](https://docs.unrealengine.com/4.27/en-US/API/Runtime/Json/Dom/FJsonObject/) class.

**You can use this to directly get a TMap with the keys “0”, “1”, “2”, etc in this example.**

I struggled with this so hope it saves you some trouble!

---

<div class="post-metadata">

**Author:** ![SirDeVeous1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SirDeVeous1](https://forums.unrealengine.com/u/SirDeVeous1)\
**Post date:** [February 13, 2025, 8:51pm UTC](https://forums.unrealengine.com/t/parse-nested-json-to-tmap-in-unreal-engine-4/157998/5 "2025-02-13T20:51:38Z")

</div>

```auto
TMap<FString, FString> URequestManagerBase::DeSerializeJsonContent(const FString& JsonContent)
{
	TMap<FString, FString> ReturnMap;

	TSharedPtr<FJsonObject> JsonObject;
	TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(JsonContent);

	if (FJsonSerializer::Deserialize(Reader, JsonObject) && JsonObject.IsValid())
	{
		for (const TPair<FString, TSharedPtr<FJsonValue>>& Pair : JsonObject->Values)
		{
			CheckJsonType(Pair, &ReturnMap);
		}
	}
	return ReturnMap;
}
void URequestManagerBase::CheckJsonType(TPair<FString, TSharedPtr<FJsonValue>> Json, TMap<FString, FString>* Map)
{
	if (Json.Value->Type == EJson::Object || Json.Value->Type == EJson::Array)
	{
		if (Json.Value->Type == EJson::Object)
		{
			for (const TPair<FString, TSharedPtr<FJsonValue>>& Pair : Json.Value->AsObject()->Values)
			{
				CheckJsonType(Pair, Map);
			}
		}
		else if (Json.Value->Type == EJson::Array)
		{
			int32 Index = 1;
			for (const auto& ArrayValue : Json.Value->AsArray())
			{
				Map->Add(Json.Key + "_" + FString::FromInt(Index), ArrayValue->AsString());
				Index++;
			}
		}
	}
	else 
	{
		Map->Add(Json.Key, Json.Value->AsString());
	}
}

```
